Created
May 14, 2016 16:01
-
-
Save giwa/5f958dde3e1bab0076c48b30f08e4ddf to your computer and use it in GitHub Desktop.
Longest common prefix ref: http://qiita.com/giwa/items/0102e19e14f756379bee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
"abcdefgh", | |
"aefghijk", | |
"abcefgh" | |
] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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