Let's make this one less mysterious. For 290. Word Pattern, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are wordPattern.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- 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//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Word Pattern.
02//Memory Usage: 8.5 MB, less than 88.89% of C++ online submissions for Word Pattern.
03
04class Solution {
05public:
06 bool wordPattern(string pattern, string str) {
07 map<char, string> m1;
08 map<string, char> m2;
09
10 string delimiter = " ";
11
12 size_t pos = 0;
13 string token;
14 int i = 0;
15 while ((pos = str.find(delimiter)) != string::npos) {
16 token = str.substr(0, pos);
17 // cout << token << endl;
18 if(m1.find(pattern[i]) == m1.end()){
19 m1[pattern[i]] = token;
20 }else if(m1[pattern[i]] != token){
21 return false;
22 }
23
24 if(m2.find(token) == m2.end()){
25 m2[token] = pattern[i];
26 }else if(m2[token] != pattern[i]){
27 return false;
28 }
29
30 str.erase(0, pos + delimiter.length());
31 i++;
32 }
33 // cout << i << endl;
34 //i means the count of space in str
35 if(i != pattern.size()-1) return false;
36
37 if(m1.find(pattern[i]) == m1.end()){
38 m1[pattern[i]] = str;
39 }else if(m1[pattern[i]] != str){
40 return false;
41 }
42
43 if(m2.find(str) == m2.end()){
44 m2[str] = pattern[i];
45 }else if(m2[str] != pattern[i]){
46 return false;
47 }
48
49 return true;
50 }
51};
52
53//https://leetcode.com/problems/word-pattern/discuss/73409/Short-C%2B%2B-read-words-on-the-fly
54
55class Solution {
56public:
57 bool wordPattern(string pattern, string str) {
58 map<char, int> p2i;
59 map<string, int> s2i;
60 int i = 0, n = pattern.size();
61
62 istringstream is(str);
63
64 //init -> (condition -> block -> increment)*n -> condition -> break
65 //so in the 1st iteration, word will be str's first token
66 for(string word; is >> word; i++){
67 //i == n: it only happens when str's token count larger than pattern length
68 if(i == n || p2i[pattern[i]] != s2i[word]){
69 return false;
70 }
71 //can't set the value to 0, because 0 is the default value
72 p2i[pattern[i]] = s2i[word] = i+1;
73 }
74
75 //i means token count now
76 return i == n;
77 }
78};
Cost