-
Notifications
You must be signed in to change notification settings - Fork 7
/
leastcommonprefix_arrayofstrings.cpp
60 lines (55 loc) · 1.11 KB
/
leastcommonprefix_arrayofstrings.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
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
public:
int get_min_length(vector <string> &A) {
int min_len = INT_MAX;
for (int i = 0; i < A.size(); ++i) {
if (A[i].size() < min_len) {
min_len = A[i].size();
}
}
return min_len;
}
bool all_contains_prefix(vector <string> &A, string str, int l, int h) {
for (int i = 0; i < A.size(); ++i) {
for (int j = l; j <= h; ++j) {
if (A[i][j] != str[j]) {
return false;
}
}
}
return true;
}
string longestCommonPrefix(vector <string> &A) {
if (A.empty()) {
return "";
}
int min_len = get_min_length(A);
string common_prefix;
int l = 0, h = min_len;
while (l <= h) {
int mid = l + (h - l)/2;
if (all_contains_prefix(A, A[0], l, mid)) {
common_prefix += A[0].substr(l, mid - l + 1);
l = mid + 1; // right part
} else {
h = mid - 1; // left part
}
}
return common_prefix;
}
};
int main() {
int N;
cin >> N;
vector <string> A(N, "");
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
Solution a;
cout << a.longestCommonPrefix(A) << endl;
return 0;
}