Scrambled by Cuts
To scramble a string of two or more letters, cut it into two non-empty parts, choose whether the two parts trade places, and then scramble each part the same way. A single letter stays as it is.
Return True if scrambling text can give goal, otherwise False.
Example 1
text "tea"
goal "eat"
returns true
Cut tea into t and ea, and let the two parts trade places.
Example 2
text "abcd"
goal "bacd"
returns true
Cut into ab and cd. Scramble ab into ba, and leave cd as it is.
Example 3
text "abcd"
goal "bdac"
returns false
Wherever abcd is cut, the letters of its left part are not together at either end of bdac.
Constraints
1 <= len(text) <= 20len(goal) == len(text)textandgoalhold lowercase letters
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Strings problems
- Shortest Packing After RemovalsHard
- Smallest After Moving LettersHard
- Smallest Order Without TwinsHard
- Acronym of WordsBeginner
- Alternating CaseBeginner
- Capitalize the TitleBeginner
- Center in a FieldBeginner
- Chunks From the LeftBeginner