leetCode 87.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 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 that "rgtae" is a scrambled string of "great".

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

思路:这一天如果理解思想之后去做感觉不是很难,但是在想到解法之前还是有一定难度。

本题解法为递归解法,动态规划解法没有掌握。

递归的思路和遍历字符串,分割成两部分,对比两部分是否能scramble,不过本题要比较前前和前后两次。

具体代码如下:

public class Solution {
    public boolean isScramble(String s1, String s2) {
    	/**
    	 * 思想是递归,将字符串逐个的分为两串
    	 * 然后让两串的前前比较、后后比较,是则返回true
    	 * 不是着前后比较,是则返回true
    	 * 如果还不是,则切割的字符串位置+1
    	 */

    	char[] c1 = s1.toCharArray();
    	char[] c2 = s2.toCharArray();

    	//调用函数判断
    	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){

    	//判断字符是否为空
    	if(end1 - start1 <= 0 && end2 - start2 <= 0){
    		return true;
    	}
    	//如果长度为1,则必须相等
    	if(end1 - start1 == 1 && end2 - start2 == 1){
    		return c1[start1] == c2[start2];
    	}
    	//长度不等返回false
    	if( end1 - start1 != end2 - start2){
    		return false;
    	}

    	int[] a = new int[128];
    	//每个字符串的字符必须个数相等
    	for(int i = 0; i < end1 - start1; i++){
    		a[c1[i+start1]]++;
    		a[c2[i+start2]]--;
    	}

    	//不相等返回false
    	for(int i = 0; i < a.length; i++){
    		if(a[i] != 0){
    			return false;
    		}
    	}
    	//递归实现
    	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;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-07 16:23:11

leetCode 87.Scramble String (拼凑字符串) 解题思路和方法的相关文章

leetCode 97.Interleaving String (交错字符串) 解题思路和方法

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 思路:此题刚开始有点想当然了

[LeetCode] 87. 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 a

(动态规划、递归) leetcode 87. Scramble String

思路:用递归来做感觉比动态规划简单,题目让我们判断s1和s2是否是scramble string,则s1上(从左起计数)有一个长度为 i 的分割点将s1分为s1_l 和 s1_r 两段 分别与 s2的(从左起计数一个长度为i的) s2_l 和 s2_r 互为 scramble string:或者与 s2的(从右起计数一个长度为i的)s2.substr(s-i) 和 s2.substr(0, s-i) 互为 scramble string . 1)C++ 中的 substr(pos, len) 表

[LeetCode] 87. Scramble String Java

题目: 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 choo

Leetcode 87 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 a

leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "

leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法

Wildcard Matching '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s,

leetCode 49.Anagrams (回文构词法) 解题思路和方法

Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路:这题要是解,必须知道什么是回文构词法.所谓回文构词法就是把一个单词的顺序调整,形成新的单词,如"eat","tea"就是回文构词法. 所以回文构词法一定是相同的字母不同的顺序,而且最少有两个单词. 本题是将单词排序之

leetCode 51.N-Queens (n皇后问题) 解题思路和方法

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur