-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-common-prefix.cpp
50 lines (42 loc) · 1.17 KB
/
longest-common-prefix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// https://leetcode.com/problems/longest-common-prefix/description/
// 1. loop each string, update the ans each time
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
string ans = strs[0];
// loop each str in strs
for (int i = 1; i < strs.size(); i++) {
// loop each char in the potential ans
for (int j = 0; j < ans.length(); j++) {
if (ans[j] == strs[i][j])
continue;
// else update ans
else
ans = ans.substr(0, j);
}
}
return ans;
}
};
// 2. sort first, then only need to compare first & last strings
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
// sort, so we only need to compare the first & last strings
sort(strs.begin(), strs.end());
int n = strs.size();
string first = strs[0];
string last = strs[n - 1];
string ans = "";
// loop each char
for (int i = 0; i < min(first.length(), last.length()); i++) {
// once the prefix are not the same
if (first[i] != last[i]) {
return ans;
}
// else, append this char to the ans
ans += first[i];
}
return ans;
}
};