[Leetcode] 87. Scramble String Java

Source: Internet
Author: User

Topic:

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 .

test instructions and analysis: A string can be recursively divided into any of two non-empty strings, arbitrarily swapping two non-leaf nodes, and getting a new string, called the scrambled Strin of the string. Given two strings S1 and S2, Judge S2 is not S1 scramble string, if S2 is S1 scramble string, then there must be a S1 on the length L1, will s1 into S11 and S12 two paragraphs, the same S21 and S22. Then there are only two possible: (1) S11 and S21 are scramble and S12 and S22 are Scramble (2) S11 and S22 are scramble and S12 and S21 are scramble

Code:

classSolution { Public Booleanisscramble (string s1, string s2) {if(S1.equals (S2))return true; intLength =s1.length (); int[] Count =New int[26];  for(inti=0;i<length;i++) {Count[s1.charat (i)-' a '] + +; Count[s2.charat (i)-' a ']--; }         for(inti=0; i<26; i++) {            if(count[i]!=0)                return false; }         for(inti=1;i<length;i++){            if(Isscramble (S1.substring (0,i), s2.substring (0, i)) && isscramble (s1.substring (i), s2.substring (i)))return true; if(Isscramble (S1.substring (0,i), s2.substring (length-i))&& isscramble (s1.substring (i), s2.substring (0,length-i)))return true; }        return false; }}

[Leetcode] 87. Scramble String Java

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.