This problem looks busy at first, but the accepted solution is built around one steady invariant. For 87. Scramble String, the solution in this repository is mainly a backtracking 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: backtracking.
The notes already sitting in the source point us in the right direction:
- backtracking
- TLE
- 6 / 285 test cases passed.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are backtrack, former, latter, isScramble, counter.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
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//backtracking
02//TLE
03//6 / 285 test cases passed.
04class Solution {
05public:
06 int n;
07 string s2;
08
09 bool backtrack(vector<string>& split){
10 if(split.size() == n){
11 string merged = "";
12 for(const string& s : split){
13 merged += s;
14 }
15 return merged == s2;
16 }else{
17 for(int i = 0; i < split.size(); ++i){
18 if(split[i].size() > 1){
19 string s = split[i];
20 vector<string> former(split.begin(), split.begin()+i);
21 vector<string> latter(split.begin()+i+1, split.end());
22 for(int j = 1; j < s.size(); ++j){
23 //split[0...i-1] + s_part1 + s_part2 + split[i+1...]
24
25 string s_part1 = s.substr(0, j);
26 string s_part2 = s.substr(j);
27
28 //not swap
29 vector<string> nsplit1 = former;
30 nsplit1.push_back(s_part1);
31 nsplit1.push_back(s_part2);
32 nsplit1.insert(nsplit1.end(), latter.begin(), latter.end());
33 if(backtrack(nsplit1)) return true;
34
35 //swap
36 vector<string> nsplit2 = former;
37 nsplit2.push_back(s_part2);
38 nsplit2.push_back(s_part1);
39 nsplit2.insert(nsplit2.end(), latter.begin(), latter.end());
40 if(backtrack(nsplit2)) return true;
41 }
42 }
43 }
44 return false;
45 }
46 }
47
48 bool isScramble(string s1, string s2) {
49 n = s1.size();
50 this->s2 = s2;
51
52 vector<string> split = {s1};
53
54 return backtrack(split);
55 }
56};
57
58//recursion
59//https://leetcode.com/problems/scramble-string/discuss/29392/Share-my-4ms-c%2B%2B-recursive-solution
60//Runtime: 4 ms, faster than 97.37% of C++ online submissions for Scramble String.
61//Memory Usage: 12 MB, less than 59.34% of C++ online submissions for Scramble String.
62class Solution {
63public:
64 bool isScramble(string s1, string s2) {
65 if(s1 == s2) return true;
66
67 int len = s1.size();
68
69 vector<int> counter(26, 0);
70 for(int i = 0; i < len; ++i){
71 ++counter[s1[i]-'a'];
72 --counter[s2[i]-'a'];
73 }
74
75 if(any_of(counter.begin(), counter.end(),
76 [](const int& e){return e != 0;})){
77 return false;
78 }
79
80 //split at 1, 2, ..., len-1
81 for(int slen = 1; slen <= len-1; ++slen){
82 //not swap
83 if(isScramble(s1.substr(0, slen), s2.substr(0, slen))
84 && isScramble(s1.substr(slen), s2.substr(slen))){
85 return true;
86 }
87
88 //swap: s1[slen...]-s2[0...len-slen-1] and
89 //s1[0...slen-1]-s2[len-slen...]
90 if(isScramble(s1.substr(slen), s2.substr(0, len-slen))
91 && isScramble(s1.substr(0, slen), s2.substr(len-slen))){
92 return true;
93 }
94 }
95
96 return false;
97 }
98};
Cost