forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain3.cpp
54 lines (38 loc) · 1.19 KB
/
main3.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
/// Source : https://leetcode.com/problems/longest-common-prefix/description/
/// Author : liuyubobobo
/// Time : 2018-06-03
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Divide and Conquer
/// Time Complexity: O(len(strs) * max len of string)
/// Space Complexity: O(log(len(strs)))
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.size() == 0)
return "";
return solve(strs, 0, strs.size() - 1);
}
private:
string solve(const vector<string>& strs, int start, int end){
if(start == end)
return strs[start];
int mid = (start + end) / 2;
string res1 = solve(strs, start, mid);
string res2 = solve(strs, mid + 1, end);
int i = 0;
for( ; i < res1.size() && i < res2.size(); i ++)
if(res1[i] != res2[i])
break;
return res1.substr(0, i);
}
};
int main() {
vector<string> strs1 = {"flower","flow","flight"};
cout << Solution().longestCommonPrefix(strs1) << endl;
vector<string> strs2 = {"dog","racecar","car"};
cout << Solution().longestCommonPrefix(strs2) << endl;
return 0;
}