-
Notifications
You must be signed in to change notification settings - Fork 0
14_LongestCommonPrefix
a920604a edited this page Apr 14, 2023
·
1 revision
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ret ;
int n = strs.size();
for(int i=0;i<strs[0].size(); ++i){
int count = 1;
for(int j = 1;j<n;++j){
if(strs[0][i] == strs[j][i]) count++;
else break;
}
if(count==n) ret+=strs[0][i];
else return ret;
}
return ret;
}
};
- time complexity
O(nm)
- space complexity
O(n)
footer