/
longest_common_prefix.cpp
72 lines (69 loc) · 1.73 KB
/
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
/*
* longest_common_prefix.cpp
* Copyright (C) 2014 zhao
*
* Distributed under terms of the MIT license.
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
struct Node{
int count;
map<char, struct Node*> children;
Node(): count(0){}
Node* insert(char value){
Node *next;
if(children.count(value) == 0){
next = new Node();
children[value] = next;
}
else{
next = children[value];
}
next->count++;
return next;
}
};
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
Node *root = new Node();
for(vector<string>::iterator it = strs.begin(); it != strs.end(); ++it){
Node *current = root;
for(int i = 0; i < (int)(*it).size(); ++i){
current = current->insert((*it)[i]);
}
}
string ans = "";
Node *current = root;
while(true){
bool valid = false;
for(map<char, struct Node*> ::iterator it = current->children.begin(); it != current->children.end(); ++it){
if(it->second->count == strs.size()){
ans += it->first;
valid = true;
current = it->second;
break;
}
}
if(valid == false){
return ans;
}
}
return ans;
}
};
int main(int argc, char *argv[])
{
Solution *t = new Solution();
string a = "ABC", b = "";
vector<string> vs;
vs.push_back(a);
vs.push_back(b);
cout<< t->longestCommonPrefix(vs) << endl;
return 0;
}