-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
string_decompositions_into_dictionary_words.cc
58 lines (49 loc) · 1.78 KB
/
string_decompositions_into_dictionary_words.cc
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
#include <string>
#include <unordered_map>
#include <vector>
#include "test_framework/generic_test.h"
using std::string;
using std::unordered_map;
using std::vector;
bool MatchAllWordsInDict(const string&, const unordered_map<string, int>&, int,
int, int);
vector<int> FindAllSubstrings(const string& s, const vector<string>& words) {
unordered_map<string, int> word_to_freq;
for (const string& word : words) {
++word_to_freq[word];
}
int unit_size = size(words.front());
vector<int> result;
for (int i = 0; i + unit_size * size(words) <= size(s); ++i) {
if (MatchAllWordsInDict(s, word_to_freq, i, size(words), unit_size)) {
result.emplace_back(i);
}
}
return result;
}
bool MatchAllWordsInDict(const string& s,
const unordered_map<string, int>& word_to_freq,
int start, int num_words, int unit_size) {
unordered_map<string, int> curr_string_to_freq;
for (int i = 0; i < num_words; ++i) {
string curr_word = s.substr(start + i * unit_size, unit_size);
if (auto iter = word_to_freq.find(curr_word); iter == end(word_to_freq)) {
return false;
} else {
++curr_string_to_freq[curr_word];
if (curr_string_to_freq[curr_word] > iter->second) {
// curr_word occurs too many times for a match to be possible.
return false;
}
}
}
return true;
}
// clang-format off
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"s", "words"};
return GenericTestMain(args, "string_decompositions_into_dictionary_words.cc", "string_decompositions_into_dictionary_words.tsv", &FindAllSubstrings,
DefaultComparator{}, param_names);
}
// clang-format on