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":

  1. great
  2. / \
  3. gr eat
  4. / \ / \
  5. g r e at
  6. / \
  7. 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".

  1. rgeat
  2. / \
  3. rg eat
  4. / \ / \
  5. r g e at
  6. / \
  7. 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".

  1. rgtae
  2. / \
  3. rg tae
  4. / \ / \
  5. r g ta e
  6. / \
  7. 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.

Example 1:

  1. Input: s1 = "great", s2 = "rgeat"
  2. Output: true

Example 2:

  1. Input: s1 = "abcde", s2 = "caebd"
  2. Output: false

一种爬行字符串,就是说假如把一个字符串当做一个二叉树的根,然后它的非空子字符串是它的子节点,然后交换某个子字符串的两个子节点,重新爬行回去形成一个新的字符串,这个新字符串和原来的字符串互为爬行字符串。

解法1: 递归Recursion

解法2: 动态规划Dynamic Programming

Java:

  1. public class Solution {
  2. public boolean isScramble(String s1, String s2) {
  3. if (s1.equals(s2)) return true;
  4.  
  5. int[] letters = new int[26];
  6. for (int i=0; i<s1.length(); i++) {
  7. letters[s1.charAt(i)-'a']++;
  8. letters[s2.charAt(i)-'a']--;
  9. }
  10. for (int i=0; i<26; i++) if (letters[i]!=0) return false;
  11.  
  12. for (int i=1; i<s1.length(); i++) {
  13. if (isScramble(s1.substring(0,i), s2.substring(0,i))
  14. && isScramble(s1.substring(i), s2.substring(i))) return true;
  15. if (isScramble(s1.substring(0,i), s2.substring(s2.length()-i))
  16. && isScramble(s1.substring(i), s2.substring(0,s2.length()-i))) return true;
  17. }
  18. return false;
  19. }
  20. }

Python:

  1. # Time: O(n^4)
  2. # Space: O(n^3)
  3. class Solution(object):
  4. # @return a boolean
  5. def isScramble(self, s1, s2):
  6. if not s1 or not s2 or len(s1) != len(s2):
  7. return False
  8. if s1 == s2:
  9. return True
  10. result = [[[False for j in xrange(len(s2))] for i in xrange(len(s1))] for n in xrange(len(s1) + 1)]
  11. for i in xrange(len(s1)):
  12. for j in xrange(len(s2)):
  13. if s1[i] == s2[j]:
  14. result[1][i][j] = True
  15.  
  16. for n in xrange(2, len(s1) + 1):
  17. for i in xrange(len(s1) - n + 1):
  18. for j in xrange(len(s2) - n + 1):
  19. for k in xrange(1, n):
  20. if result[k][i][j] and result[n - k][i + k][j + k] or\
  21. result[k][i][j + n - k] and result[n - k][i + k][j]:
  22. result[n][i][j] = True
  23. break
  24.  
  25. return result[n][0][0]  

C++: Recursion

  1. class Solution {
  2. public:
  3. bool isScramble(string s1, string s2) {
  4. if(s1==s2)
  5. return true;
  6.  
  7. int len = s1.length();
  8. int count[26] = {0};
  9. for(int i=0; i<len; i++)
  10. {
  11. count[s1[i]-'a']++;
  12. count[s2[i]-'a']--;
  13. }
  14.  
  15. for(int i=0; i<26; i++)
  16. {
  17. if(count[i]!=0)
  18. return false;
  19. }
  20.  
  21. for(int i=1; i<=len-1; i++)
  22. {
  23. if( isScramble(s1.substr(0,i), s2.substr(0,i)) && isScramble(s1.substr(i), s2.substr(i)))
  24. return true;
  25. if( isScramble(s1.substr(0,i), s2.substr(len-i)) && isScramble(s1.substr(i), s2.substr(0,len-i)))
  26. return true;
  27. }
  28. return false;
  29. }
  30. };

C++: Recursion

  1. class Solution {
  2. public:
  3. bool isScramble(string s1, string s2) {
  4. if (s1.size() != s2.size()) return false;
  5. if (s1 == s2) return true;
  6. string str1 = s1, str2 = s2;
  7. sort(str1.begin(), str1.end());
  8. sort(str2.begin(), str2.end());
  9. if (str1 != str2) return false;
  10. for (int i = 1; i < s1.size(); ++i) {
  11. string s11 = s1.substr(0, i);
  12. string s12 = s1.substr(i);
  13. string s21 = s2.substr(0, i);
  14. string s22 = s2.substr(i);
  15. if (isScramble(s11, s21) && isScramble(s12, s22)) return true;
  16. s21 = s2.substr(s1.size() - i);
  17. s22 = s2.substr(0, s1.size() - i);
  18. if (isScramble(s11, s21) && isScramble(s12, s22)) return true;
  19. }
  20. return false;
  21. }
  22. };  

C++: DP

  1. class Solution {
  2. public:
  3. bool isScramble(string s1, string s2) {
  4. if (s1.size() != s2.size()) return false;
  5. if (s1 == s2) return true;
  6. int n = s1.size();
  7. vector<vector<vector<bool> > > dp (n, vector<vector<bool> >(n, vector<bool>(n + 1, false)));
  8. for (int i = 0; i < n; ++i) {
  9. for (int j = 0; j < n; ++j) {
  10. dp[i][j][1] = s1[i] == s2[j];
  11. }
  12. }
  13. for (int len = 2; len <= n; ++len) {
  14. for (int i = 0; i <= n - len; ++i) {
  15. for (int j = 0; j <= n - len; ++j) {
  16. for (int k = 1; k < len; ++k) {
  17. if ((dp[i][j][k] && dp[i + k][j + k][len - k]) || (dp[i + k][j][len - k] && dp[i][j + len - k][k])) {
  18. dp[i][j][len] = true;
  19. }
  20. }
  21. }
  22. }
  23. }
  24. return dp[0][0][n];
  25. }
  26. };

C++:

  1. class Solution {
  2. public:
  3. bool isScramble(string s1, string s2) {
  4. if (s1.size() != s2.size()) return false;
  5. if (s1 == s2) return true;
  6. int n = s1.size();
  7. vector<vector<vector<bool> > > dp (n, vector<vector<bool> >(n, vector<bool>(n + 1, false)));
  8. for (int i = n - 1; i >= 0; --i) {
  9. for (int j = n - 1; j >= 0; --j) {
  10. for (int k = 1; k <= n - max(i, j); ++k) {
  11. if (s1.substr(i, k) == s2.substr(j, k)) {
  12. dp[i][j][k] = true;
  13. } else {
  14. for (int t = 1; t < k; ++t) {
  15. if ((dp[i][j][t] && dp[i + t][j + t][k - t]) || (dp[i][j + k - t][t] && dp[i + t][j][k - t])) {
  16. dp[i][j][k] = true;
  17. break;
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. return dp[0][0][n];
  25. }
  26. };

  

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 87. Scramble String 爬行字符串的更多相关文章

  1. [LeetCode] 87. Scramble String 搅乱字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  2. [leetcode]87. Scramble String字符串树形颠倒匹配

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  3. [LintCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  4. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

  5. [LeetCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  6. leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  8. leetcode@ [87] Scramble String (Dynamic Programming)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  9. 【一天一道LeetCode】#87. Scramble String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. java调用c++库

    c++ 写的库 jni封装一层 才可以给 java调用

  2. word2vec中的subsampling

    http://d0evi1.com/word2vec-subsampling/ 为了度量这种罕见词与高频词间存在不平衡现象,我们使用一个简单的subsampling方法:训练集中的每个词wiwi,以下 ...

  3. 在Windows10系统下安装Oracle 11g数据库

    在Windows10系统下安装Oracle 11g数据库 https://blog.csdn.net/wei1992_6/article/details/60054727

  4. wordpress调用自定义菜单

    wordpress要调用自定义菜单首先要注册菜单,将代码添加到主题文件夹下的function.php中,比如wordpress自带主题2019的定义如下 // This theme uses wp_n ...

  5. cookie插件|jq-cookie.js|使用详解

    1.设置一二级域名共用的cookie:设置domain为一级域名,可一.二级域名共用的cookie $.cookie('f_city','北京|101010100|,锦州|101070701|',{e ...

  6. 笨办法学Python

    打印:%r%r 与 %s 的区别就好比 repr() 函数处理对象与 str() 函数处理对象的差别.%s => str(),比较智能%r => repr(),处理较为简单和直接 from ...

  7. 洛谷 P1908 逆序对 题解

    每日一题 day43 打卡 Analysis 因为数据规模,所以我们需要对其进行离散化,新创一个数组a里面来放在我们的初始序列中在这个位置上的数是第几大的这里还要用一个小技巧排序,关于离散化的技巧我们 ...

  8. 20-ESP8266 SDK开发基础入门篇--C# TCP客户端编写 , 加入数据通信

    https://www.cnblogs.com/yangfengwu/p/11192594.html 自行调整页面 连接上以后主动发个数据 namespace TCPClient { public p ...

  9. Connection to newtaotao failed. [08001] Could not create connection to database

    jdbc.url=jdbc:mysql://localhost:3306/newtaotao?serverTimezone=UTC&characterEncoding=utf-8 数据库是5. ...

  10. Go-Json操作

    /** * @Author: jadeshu * @Description: * @File: main * @Version: 1.0.0 * @Date: 2019/11/7 2:33 */ pa ...