Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int len_s = strs.size();
if(len_s == 0) return "";
string prefix = strs[0];
for(int i = 1; i < len_s; i++){
int minlen = min(prefix.size(), strs[i].size());
int j = 0;
for(; j < minlen; j++){
if(prefix[j] != strs[i][j])
break;
}
prefix = strs[0].substr(0, j);
}
return prefix;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment