-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_longest_common_prefix.cpp
More file actions
58 lines (40 loc) · 1.19 KB
/
14_longest_common_prefix.cpp
File metadata and controls
58 lines (40 loc) · 1.19 KB
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
/*
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:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of only lower-case English letters.
*/
#include <iostream>
#include <vector>
std::string longestCommonPrefix(std::vector<std::string>& strs);
int main() {
std::vector<std::string> strs1 = {"ab", "a"}; //, "staopi", "staapoiddoi"};
std::cout << longestCommonPrefix(strs1);
}
std::string longestCommonPrefix(std::vector<std::string>& strs) {
if (strs.size() == 1) {
return strs[0];
}
std::string s = strs[0];
int len = s.length();
std::string prefix;
for (int i = 1; i < len; i++) {
prefix = s.substr(0, i);
std::cout << prefix << std::endl;
for (std::string str : strs) {
if (str.substr(0, i).compare(prefix) != 0) {
return prefix.substr(0, i);
}
}
}
return "";
}