14. Longest Common Prefix

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;

}
};

analysis

  • time complexity O(nm)
  • space complexity O(n)