Skip to content

Instantly share code, notes, and snippets.

@giwa
Created May 14, 2016 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giwa/5f958dde3e1bab0076c48b30f08e4ddf to your computer and use it in GitHub Desktop.
Save giwa/5f958dde3e1bab0076c48b30f08e4ddf to your computer and use it in GitHub Desktop.
[
"abcdefgh",
"aefghijk",
"abcefgh"
]
string getPrefix(string a, string b){
int l = min(a.size(), b.size());
string r="";
for (int i; i < l; i++){
if (a[i] == b[i]){
string t{a[i]};
r = r + t;
}
}
return r;
}
string longestCommonPrefix(vector<string> &A) {
string r = A[0];
for (int i = 1; i < A.size(); i++){
r = getPrefix(r, A[i]);
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment