Description

LINK:

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

1
2
Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

1
2
3
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static int _ = []() {
ios::sync_with_stdio(false);
cin.tie();
return 0;
}();

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty() || strs[0].empty()) return "";
if (strs.size() == 1) return strs[0];
int len = 0;
char ch = strs[0][0];
while (true) {
int i;
for (i = strs.size() - 1; i > 0 && strs[i][len] == ch; i--);
if (i == 0) ch = strs[0][++len];
else break;
}
return strs[0].substr(0, len);
}
};

Discussion