Scramble string of leetcode

Source: Internet
Author: User

Scramble string

Given a string S1, we may represent it as a binary tree by partitioning it to two 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 may choose any non-leaf node and swap its two children.

For example, if we choose the node"gr"And swap its two children, it produces a scrambled string"rgeat".

    rgeat   /      rg    eat / \    /  r   g  e   at           /           a   t

We say that"rgeat"Is a scrambled string"great".

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

    rgtae   /      rg    tae / \    /  r   g  ta  e       /       t   a

We say that"rgtae"Is a scrambled string"great".

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

Analysis: Dynamic Planning. DP [I] [J] [k] indicates S1 [I ~ I + Len] and S2 [J ~ J + Len] whether it is scramble string, that is, whether S1 starts from I and S2 starts from J to scramble string. For each split point, determine whether the left and right sides are scramble strings. If one of them is satisfied, the substring will satisfy scramble string, that is

DP [I] [J] [Len] = (DP [I] [J] [k] & DP [I + k] [J + k] [Len-K]) | (DP [I] [J + len-K] [k] & DP [I + k] [J] [Len-K]), when Len = 1, DP [I] [J] [1] = S1 [I] = S2 [J].

Class solution {public: bool isscramble (string S1, string S2) {int length = s1.size (), I, J, K, Len; If (length! = S2.size () return false; vector <bool> dp (length); for (I = 0; I <length; ++ I) {vector <bool> tmp1 (length); For (j = 0; j <length; j ++) {vector <bool> tmp2 (Length + 1, false); tmp1 [J] = tmp2;} DP [I] = tmp1;} For (LEN = 1; Len <= length; Len ++) {for (I = 0; I + Len <= length; ++ I) {for (j = 0; j + Len <= length; ++ J) {If (LEN = 1) DP [I] [J] [Len] = (S1 [I] = S2 [J]); else {for (K = 1; k <Len; k ++) {If (DP [I] [J] [k] & DP [I + k] [J + k] [Len-K]) | (DP [I] [J + len-K] [k] & DP [I + k] [J] [Len-K]) {DP [I] [J] [Len] = true; // represents S1 [I ~ I + Len] and S2 [J ~ J + Len] whether it is scramble string break; }}}} return DP [0] [0] [length] ;}};



Scramble string of leetcode

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.