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 = strs.size();
string finalStr;
if(len == 0) return "";
sort(strs.begin(), strs.end());
int n = strs[0].size();
for(int i = 0; i<n; i++)
{
if(strs[0][i] == strs[len-1][i]){
finalStr += strs[0][i];
}
else
break;
}
return finalStr;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment