[Leetcode] Scramble String @python

Source: Internet
Author: User

Given A string S1, we may represent it as a binary tree by partitioning it to the Non-empty substrings Recursivel Y.

Below is one possible representation of S1 = "great" :

    Great   /      gr    eat/\    /  g   r  E   at           /           a   t

To scramble the string, we are choose any non-leaf node and swap it to the children.

For example, if we choose the node "gr" and swaps its-children, it produces a scrambled string "rgeat" .

    Rgeat   /      RG    eat/\    /  r   G  e   at           /           a   t

We say is "rgeat" a scrambled string of "great" .

Similarly, if we continue to swap the children of nodes "eat" "at" and, it produces a scrambled string "rgtae" .

    Rgtae   /      RG    tae/\    /  r   G  ta  e       /       t   a

We say is "rgtae" a scrambled string of "great" .

Given strings S1 and S2 of the same length, determine if S2 is a scrambled string of S1.

A good question for the string. The explanation of the problem is very complicated, which makes people overwhelmed.

What exactly does this problem mean? The end result is that the order of the letters in a string is scrambled, allowing you to tell if a string can be scrambled by another string. That upsets the process of how to do it, very simple, to give you a string, you have to find a point to cut it in half, you can exchange these two halves in order to disrupt the order of the source string, that is, in the two halves of the character and the other half of all characters in the relative order is unified. For each half, you can repeat the process above.

Think about it, how do you know where the point of interruption is? Poor lift. How do you know if there is an exchange operation after the interruption? Two cases recursion, there is a way to pass on it. Another question is that the characters contained in the two string must be exactly the same, how do you determine this? The most violent way, newly opened two strings, sorted, judged that these two new phases are unequal.

For example: "ABCD", "BDAC" is not scramble string

Code:

classSolution:#@return A Boolean    defisscramble (self, S1, S2):ifS1==S2:returnTrueifSorted (S1)! = sorted (S2):returnFalse#"ABCD", "BDAC" is not scramble stringLength=Len (S1) forIinchRange (1, length):ifSelf.isscramble (S1[:i],s2[:i]) andSelf.isscramble (s1[i:],s2[i:]):returnTrueifSelf.isscramble (s1[:i],s2[length-i:]) andSelf.isscramble (S1[i:],s2[:length-i]):returnTruereturnFalse

[Leetcode] Scramble String @python

Related Article

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.