-
Notifications
You must be signed in to change notification settings - Fork 8
/
leetcode14_longest_common_prefix.cpp
82 lines (68 loc) · 1.83 KB
/
leetcode14_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @file leetcode14_longest_common_prefix.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/06/03 11:43
* @brief https://leetcode.com/problems/longest-common-prefix
*
**/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
std::string ret;
size_t size = strs.size();
if (size == 0) {
return ret;
}
if (size == 1) {
return strs[0];
}
size_t minStrLen = INT_MAX;
for (size_t i = 0; i < size; ++i) {
size_t len = strs[i].length();
if (minStrLen> len) {
minStrLen = len;
}
}
for (size_t i = 0; i < minStrLen; ++i) {
char ch = strs[0][i];
bool same = true;
for (size_t j = 1; j < size; ++j) {
if (strs[j][i] != ch) {
same =false;
break;
}
}
if (same) {
ret += ch;
} else {
break;
}
}
return ret;
}
};
int main() {
while (1) {
std::cout << "Number of strings (Ctrl-C to exit): ";
int n;
if (!(std::cin >> n)) {
return 0;
}
std::string s;
std::getline(std::cin, s);
std::vector<std::string> strs;
std::cout << n << " strings: " << std::endl;
for (int i = 0; i < n; ++i) {
std::getline(std::cin, s);
strs.emplace_back(s);
}
Solution solution;
auto ret = solution.longestCommonPrefix(strs);
std::cout << ret << std::endl;
}
return 0;
}