Example:
["abc", "a", "ab"] --> "a"
["abcd", "ab", "abc] --> "ab"
Solution:
string lcs (vector<string> array) {
if (array.empty())
return "";
int pos = 0;
string result = "";
for (int i = 0; i < array[0].size(); i++) {
char target = array[0][i];
for (int j = 1; j < array.size(); j++) {
if (i >= array[j].size() || array[j][i] != target)
//a small trick here is that the length of the first string may be longer that some of other
//strings in the array. We need to test whether we have reached the end of other strings
return result;
else
result += array[0][i];
}
}
return result;
}
No comments:
Post a Comment