Source of the topic:
https://leetcode.com/problems/scramble-string/
Test Instructions Analysis:
Given a string, the string is turned into a binary tree, and if the binary tree is one or more of the left and right sub-trees, the new string is called scramble. Give two strings to determine if each is scramble.
Topic Ideas:
This is a dynamic programming problem in which the characters are split into two parts, and if the two parts satisfy the scramble, then the entire string satisfies the scramble.
Code (Python):
classsolution (object):defisscramble (self, S1, S2):""": Type S1:str:type s2:str:rtype:bool""" ifLen (S1)! =len (S2):returnFalseifS1 = =S2:returnTrue TMP1,TMP2=list (S1), List (S2) tmp1.sort (); Tmp2.sort ()ifTmp1! =TMP2:returnFalse Size=Len (S1) forIinchRange (1, size):ifSelf.isscramble (s1[i:],s2[i:]) andself.isscramble (s1[:i],s2[:i]):returnTrueifSelf.isscramble (S1[i:],s2[:size-i]) andSelf.isscramble (S1[:i],s2[size-I:]):returnTruereturnFalse
View Code
[Leetcode] (python): 087-scramble String