-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindSubstrings.cpp
113 lines (100 loc) · 3.56 KB
/
findSubstrings.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Coding challenge "findSubstrings" from Code Signal */
class Trie
{
struct TrieNode {
TrieNode* children[58];
bool is_word{false};
};
public:
Trie() {
root = new TrieNode;
for (int i = 0; i < 58; i++) {
root->children[i] = nullptr;
}
}
void init(TrieNode* trie_node) {
for (int i = 0; i < 58; i++) {
trie_node->children[i] = nullptr;
}
}
/** Inserts a word into the trie. */
void insert(std::string word) {
TrieNode* cur = root;
for (int i = 0; i < word.length(); i++) {
if (cur->children[word[i]-'A'] == nullptr) {
cur->children[word[i]-'A'] = new TrieNode;
init(cur->children[word[i]-'A']);
}
if (i == word.length()-1) {
cur->children[word[i]-'A']->is_word = true;
}
cur = cur->children[word[i]-'A'];
}
}
/** Returns if the word is in the trie. */
bool search(string word) {
TrieNode* cur = root;
for (int i = 0; i < word.length(); i++) {
if (cur->children[word[i]-'A'] == nullptr) {
return false;
}
if (i == word.length()-1 && !cur->children[word[i]-'A']->is_word) {
return false;
}
cur = cur->children[word[i]-'A'];
}
return true;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(std::string prefix) {
TrieNode* cur = root;
for (int i = 0; i < prefix.length(); i++) {
if (cur->children[prefix[i]-'A'] == nullptr) {
return false;
}
cur = cur->children[prefix[i]-'A'];
}
return true;
}
private:
TrieNode* root{nullptr};
};
std::vector<std::string> findSubstrings(std::vector<std::string> words, std::vector<std::string> parts) {
Trie* root = new Trie();
std::vector<std::string> result;
for (int i = 0; i < parts.size(); i++) {
root->insert(parts[i]);
}
for (int i = 0; i < words.size(); i++) {
int max_length = INT_MIN;
std::string final_string("");
int max_length_so_far = 0;
int position = -1;
for (int j = 0; j < words[i].length(); j++) {
std::string letter = words[i].substr(j,1);
if (root->startsWith(letter)) {
std::string final_value_for_letter("");
for (int k = 1; k <= words[i].length()-j; k++) {
std::string tmp = words[i].substr(j,k);
if (root->search(tmp)) {
if (max_length_so_far < k) {
max_length_so_far = k;
final_string = words[i].substr(j,k);
position = j;
}
}
}
max_length = max(max_length,max_length_so_far);
}
}
if (final_string.empty()) {
result.emplace_back(words[i]);
}
else {
int end = words[i].length() - (position+final_string.length());
std::string tmp = words[i].substr(0,position) + "[" + final_string + "]" + words[i].substr(position+final_string.length(),end);
result.emplace_back(tmp);
}
}
return result;
}