forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
47 lines (33 loc) · 1018 Bytes
/
main.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
/// Source : https://leetcode.com/problems/longest-common-prefix/description/
/// Author : liuyubobobo
/// Time : 2018-06-03
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Vertical Scan
/// Time Complexity: O(len(strs) * max len of string)
/// Space Complexity: O(1)
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string res = "";
if(strs.size() == 0)
return res;
for(int i = 0 ; i < strs[0].size() ; i ++){
char c = strs[0][i];
for(int j = 1 ; j < strs.size() ; j ++)
if(i >= strs[j].size() || strs[j][i] != c)
return res;
res += c;
}
return res;
}
};
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;
}