-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14-longest-common-prefix.cpp
51 lines (44 loc) · 1.25 KB
/
14-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
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <map>
#include <algorithm>
// performance measure header
#include <iomanip>
#include <chrono>
using namespace std;
// https://leetcode.com/problems/longest-common-prefix/
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
map<string, int> m;
for (size_t x = 0; x < strs.size(); ++x)
for (size_t y = 0; y < strs[x].size(); ++y)
++m[strs[x].substr(0, y+1)];
for (auto itr = m.rbegin(); itr != m.rend(); ++itr)
if (itr->second == int(strs.size()))
return itr->first;
return {};
}
};
int main() {
string s;
int n;
vector<string> strs;
Solution sol;
cout << "Insert the number of strings: ";
cin >> n;
cout << "Insert " << n << " strings: ";
while (n-- > 0) {
cin >> s;
strs.push_back(s);
}
auto t1{chrono::high_resolution_clock::now()};
string ls{sol.longestCommonPrefix(strs)};
auto t2{chrono::high_resolution_clock::now()};
chrono::duration<double, milli> ms_double{t2 - t1};
cout << setprecision(17) << ms_double.count() << " ms\n";
cout << ls << '\n';
return 0;
}