Leetcode 87.Scramble String (patchwork string) ideas and methods for solving problems

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 swap its-children, it produces a scrambled string < Code style= "Font-family:menlo,monaco,consolas, ' Courier New ', monospace; font-size:12.6000003814697px; PADDING:2PX 4px; Color:rgb (199,37,78); Background-color:rgb (249,242,244) ">" Rgeat ".

 Rgeat/rg eat/\/R g E at/a t 

we say That " Rgeat "  is a scrambled string Of " 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 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 .


Idea: It is not difficult to feel after thinking about this day, but it is difficult to think of a solution.

This method is recursive solution, and the dynamic programming solution is not mastered.

Recursive thinking and traversal of the string, divided into two parts, compare whether the two parts can be scramble, but the subject to compare before and before and after two times.

The specific code is as follows:

public class Solution {public Boolean isscramble (string s1, string s2) {/** * thought is recursive, dividing strings into two strings each * and then letting two strings of front    Compare, post-compare, yes returns true * not before and after comparison, yes returns true * if not yet, then cut the string position +1 */char[] C1 = S1.tochararray ();    char[] C2 = S2.tochararray ();    Call function to determine if (ISSCR (c1, c2, 0, c1.length, 0, c2.length)) {return true;    } return false; } Boolean ISSCR (char[] c1,char[] c2,int start1,int end1,int start2, int end2) {//Determine if character is empty if (end1-start    1 <= 0 && end2-start2 <= 0) {return true;    }//If the length is 1, it must be equal if (End1-start1 = = 1 && end2-start2 = = 1) {return C1[start1] = = C2[start2];    }//Length not equal to return FALSE if (end1-start1! = End2-start2) {return false;    } int[] A = new int[128];    The characters of each string must be equal in the number of for (int i = 0; i < End1-start1; i++) {a[c1[i+start1]]++;    a[c2[i+start2]]--;    }//Unequal returns false for (int i = 0; i < a.length; i++) {if (a[i]! = 0) {return false; }}//RecursiveImplement for (int i = 1; i < End1-start1; i++) {if (ISSCR (c1, C2, Start1, Start1+i, Start2, Start2+i) && ISSCR (    C1, c2, Start1+i, End1, Start2+i, End2) {return true;    } if ((ISSCR (c1, C2, Start1, Start1+i, End2-i, End2) && ISSCR (C1, C2, Start1+i, End1, Start2, end2-i))) {    return true;    }} return false; }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 87.Scramble String (patchwork string) ideas and methods for solving problems

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.