-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30. Substring with Concatenation of All Words.cpp
More file actions
55 lines (49 loc) · 1.93 KB
/
Copy path30. Substring with Concatenation of All Words.cpp
File metadata and controls
55 lines (49 loc) · 1.93 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
50
51
52
53
54
55
//https://leetcode.com/problems/substring-with-concatenation-of-all-words/
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
/**
Since the order of words can be in any form in the given string.
1. Create a lookup table for each word and there count.
2. There may be a case same word is repitative in the given words list.
3. Next start from index 0 and check a sub-str of given word length is found in the lookup table. If so decrement the counter for that in the lookup table.
4. Once all words in lookup table is found, update the index accordingly.
**/
vector<int> result;
if ( s.empty() || words.empty()) {
return result;
}
const int wLen = words[0].size();
const int totalWordsLength = words.size() * wLen;
if ( totalWordsLength > s.size()) {
return result;
}
// Create a lookup table for all words.
unordered_map<string, int> wLookup;
for(auto word : words) {
wLookup[word]++;
}
for(int i = 0 ; i <= s.size() - totalWordsLength; i++) {
auto cLookup = wLookup;
int cStart = i;
while(!cLookup.empty() && cStart < s.size()) {
string cStr = s.substr(cStart,wLen);
auto it = cLookup.find(cStr);
if ( it != cLookup.end()) {
it->second -= 1;
if ( it->second == 0 ) {
cLookup.erase(it);
}
cStart += wLen;
} else {
break;
}
}
// Check if cLookup is empty. then update the result and i.
if ( cLookup.empty()) {
result.push_back(i);
}
}
return result;
}
};