【leetcode】Scramble String

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.

1. 如果两个substring相等的话,则为true
2. 如果两个substring中间某一个点,左边的substrings为scramble string, 同时右边的substrings也为scramble string,则为true
3. 如果两个substring中间某一个点,s1左边的substring和s2右边的substring为scramble string, 同时s1右边substring和s2左边的substring也为scramble string,则为true

 1 class Solution {
 2 public:
 3     bool isScramble(string s1, string s2) {
 4
 5         int n=s1.length();
 6         vector<vector<vector<bool>>> dp(n,vector<vector<bool>>(n,vector<bool>(n+1)));
 7         //dp[i][j][k] represent whether s1[i,i+1,...,i+k-1] and  s2[j,j+1,...,j+k-1] is scramble
 8
 9
10         for(int i=n-1;i>=0;i--)
11         {
12             for(int j=n-1;j>=0;j--)
13             {
14                 for(int k=1;k<=n-max(i,j);k++)
15                 {
16                     if(s1.substr(i,k)==s2.substr(j,k))
17                     {
18                         dp[i][j][k]=true;
19                     }
20                     else
21                     {
22                         for(int l=1;l<k;l++)
23                         {
24                             if(dp[i][j][l]&&dp[i+l][j+l][k-l]||dp[i][j+k-l][l]&&dp[i+l][j][k-l])
25                             {
26                                 dp[i][j][k]=true;
27                                 break;
28                             }
29                         }
30
31                     }
32                 }
33             }
34
35         }
36         return dp[0][0][n];
37     }
38 };
时间: 2024-10-24 21:52:12

【leetcode】Scramble String的相关文章

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【LeetCode】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】Reverse String

题目链接:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 思路: easy 算法: public String reverseString(String s) { char

【LeetCode】008 String to Integer (atoi)

题目:LeetCode 008 String to Integer 题意:完成内置函数atoi的功能,将字符串转换成整数. 教训:一开始理所应当的随便一写,然后发现有很多的异常情况需要处理.然后按照C++ Reference中关于atoi的规定一条一条写,才AC.另外还有一个溢出的问题,一开始以为int会自动处理直接返回边界值,其实不是,如果溢出的话大于2147483647的数会给变成负数,因此要单独判断是否会大,但是设置成longlong 之后出现的问题是,还有可能会溢出longlong,所以

【leetcode】Interleaving String

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】8. String to Integer (atoi) 字符串转整数

题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be

【LeetCode】8 - String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leetcode】 Interleaving String (hard)

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】443. String Compression

题目如下: Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After you are done modifying th