-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0014_LongestCommonPrefix.cpp
More file actions
49 lines (46 loc) · 1.26 KB
/
Copy path0014_LongestCommonPrefix.cpp
File metadata and controls
49 lines (46 loc) · 1.26 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
#include <fmt/ranges.h>
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
string longestCommonPrefix(vector<string> &strs)
{
// boundary case
if (strs.size() == 1) {
return strs[0];
}
int n = 200;
for (std::string_view str : strs) {
n = std::min(n, static_cast<int>(str.size()));
}
int i = 0;
for (; i < n; ++i) {
bool matched = true;
for (int j = 0; j < strs.size() - 1; ++j) {
matched &= strs[j][i] == strs[j + 1][i];
if (not matched)
return strs[0].substr(0, i);
}
}
// boundary case n=0
if (n == 0 or i == 0) {
return "";
}
return strs[0].substr(0, i);
}
};
int main()
{
Solution sol;
std::vector<string> strs1 = {"flower", "flow", "flight"};
std::vector<string> strs2 = {"dog", "racecar", "car"};
std::vector<string> strs3 = {"a"};
std::vector<string> strs4 = {"a", "ab"};
for (vector<string> str : {strs1, strs2, strs3, strs4}) {
fmt::print("set: [{}] longestCommonPrefix: {}\n",
fmt::join(str, ","),
sol.longestCommonPrefix(str));
}
}