Showing posts with label longest prefix among strings. Show all posts
Showing posts with label longest prefix among strings. Show all posts

Sunday, October 11, 2015

Find longest prefix among strings

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string longestCommonPrefix(vector<string> &A) {
    int in=A[0].length()-1;
   // cout<<in;
    for(int i=0;i<A.size()-1;i++)
    {
        cout<<in<<endl;
        while(A[i][in]!=A[i+1][in]) {
            cout<<"equal";
          in--;
          }
    }
    string str;
    str=A[0].substr(0,in+1);
    return str;
    }
int main()
{
    vector <string> v;
    v.push_back("hello");
    v.push_back("helpp");
    v.push_back("heloo");
    string str=longestCommonPrefix(v);
    cout<<str;
}

Contributors

Translate