"One Day together Leetcode" #87. Scramble String

Source: Internet
Author: User

One Day together Leetcode

This series of articles has all been uploaded to my github address: Zeecoder ' s GitHub
You are welcome to follow my Sina Weibo, my Sina Weibo blog
Welcome reprint, Reprint please indicate the source

(i) Title

Given A string s1, we may be represent it as a binary tree by partitioning it to the non-empty substrings recursively.

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 this "rgeat" is a scrambled string of "great".

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

Rgtae
/ \
RG Tae
/ \ / \
R g Ta E
/ \
T A

We say this "Rgtae" is a scrambled string of "great".

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

(ii) Problem solving

The main idea: Judge two string is not scramble string, the topic is a bit long, need to read a long time!!!
To give a simple example of scramble string, such as AB and BA, then AB splits into a and b,ba split into B and A, which represents the scramble string.
Then whether S1 and S2 are scramble strings, it is necessary to split the S1 and S2 into two parts S11 and S12,S21 and S22, which need to be judged whether the two parts are scramble string.
Can be divided into two parts: (S11 and S21,S12 and S22) as well as (S11 and S22,S12 and S21).
So the code is better written!
The dynamic programming problem is obvious. But I do the problem in the process of optimization is still not done, resulting in timeouts.

classSolution { Public:BOOLIsscramble (stringS1,stringS2) {intLen1 = S1.length ();intLen2 = S2.length ();if(Len1! = len2)return false;//Length not the same        if(S1 = = s2)return true;//substring equal        //Determine if two substrings contain the same characters        //Did not add this step at first, resulting in a time-out         vector<int>Count -,0); for(inti =0; i < len1; i++) {count[s1[i]-' A ']++; count[s2[i]-' A ']--; } for(inti =0; I < -; i++) {if(count[i]!=0)return false; }//Recursive         for(inti =1; i < len1; i++) {stringSubs1_1 = S1.substr (0, i);stringSubs1_2 = S1.substr (i);stringSubs2_1 = S2.substr (0, i);stringSubs2_2 = S2.substr (i);BOOLIs1 = (subs1_1==subs2_1?true: Isscramble (Subs1_1, Subs2_1)) && (subs1_2==subs2_2?true: Isscramble (Subs1_2, subs2_2));//The recursion is also optimized here, and the equivalent is not recursiveSubs2_1 = S2.substr (len2-i, i); Subs2_2 = S2.substr (0, len2-i);BOOLIs2 = (subs1_1==subs2_1?true: Isscramble (Subs1_1, Subs2_1)) && (subs1_2==subs2_2?true: Isscramble (Subs1_2, subs2_2));if(Is1 | | is2)return true;//Two part of scramble string S1 and S2 are scramble string}return false; }};

"One Day together Leetcode" #87. 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.