[Leetcode] (python): 087-scramble String

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.