Let's make this one less mysterious. For 30. Substring with Concatenation of All Words, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- https://leetcode.com/problems/substring-with-concatenation-of-all-words/discuss/13658/Easy-Two-Map-Solution-(C%2B%2BJava)
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are findSubstring, subs.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//https://leetcode.com/problems/substring-with-concatenation-of-all-words/discuss/13658/Easy-Two-Map-Solution-(C%2B%2BJava)
02//Runtime: 544 ms, faster than 56.37% of C++ online submissions for Substring with Concatenation of All Words.
03//Memory Usage: 23.1 MB, less than 56.80% of C++ online submissions for Substring with Concatenation of All Words.
04class Solution {
05public:
06 vector<int> findSubstring(string s, vector<string>& words) {
07 int n = s.size();
08 int m = words.size();
09
10 vector<int> ans;
11
12 if(m == 0){
13 return ans;
14 }
15
16 int l = words[0].size();
17
18 unordered_map<string, int> word2count;
19
20 for(string& word : words){
21 ++word2count[word];
22 }
23
24 //m*l: words' total length
25 for(int i = 0; i + m*l - 1 < n; ++i){
26 int j; //index to "words"
27
28 unordered_map<string, int> word2visited;
29
30 for(j = 0; j < m; ++j){
31 string subs(s.begin()+i+j*l, s.begin()+i+(j+1)*l);
32 if(word2count.find(subs) == word2count.end()){
33 break;
34 }
35 ++word2visited[subs];
36 if(word2visited[subs] > word2count[subs]){
37 break;
38 }
39 }
40
41 if(j == m){
42 ans.push_back(i);
43 }
44 }
45
46 return ans;
47 }
48};
Cost