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.
递归的来搞。比较粗暴。
class Solution { public: bool isScramble(string s1, string s2) { int n = s1.size(); if (!isSameChar(s1, s2)) return false; if (s1 == s2) return true; for (int i = 1; i < n; i++) { string s11 = s1.substr(0, i); string s12 = s1.substr(i); string s21 = s2.substr(0, i); string s22 = s2.substr(i); if (isScramble(s11, s21) && isScramble(s12, s22)) return true; s21 = s2.substr(0, n - i); s22 = s2.substr(n - i); if (isScramble(s11, s22) && isScramble(s12,s21)) return true; } return false; } bool isSameChar(string a, string b) { int c[256] = {0}; for (int i = 0; i < a.size(); i++) { c[a[i]]++; } for (int i = 0; i < b.size(); i++) { if (--c[b[i]] < 0) return false; } return true; } };
dp[i][j][k] means : s1[i~i+k], s2[j~j+k] is scramble.
// dp[i][j][k] =
// (dp[i][j][t] && dp[i+t][j+t][k-t]) ||
// (dp[i][j+k-t][t] &&dp[i+t][j][k-t]), (1<= t <= k-1)
Note:
loop need to begin from n - 1 to 0, if not, the result is not correct.
because s[0][0][n] need k from 1~n-1, so s[1][1][n-1] must be in the front of s[0][0][n].
class Solution { public: bool isScramble(string s1, string s2) { int n = s1.size(); bool dp[100][100][100] = {0}; memset(dp, 0, sizeof(dp)); // dp[i][j][k] // if true, s1[i~i+k], s2[j~j+k] match. // else dismatch. // dp[i][j][k] = // (dp[i][j][t] && dp[i+t][j+t][k-t]) || // (dp[i][j+k-t][t] &&dp[i+t][j][k-t]), (1<= t <= k-1) for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dp[i][j][0] = true; dp[i][j][1] = (s1[i] == s2[j]); } } for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { for (int k = 2; (i+k <= n) && (j+k <= n); k++) { for (int t = 1; t < k; t++) { if (dp[i][j][k]) break; dp[i][j][k] |= (dp[i][j][t] && dp[i+t][j+t][k-t]) || (dp[i][j+k-t][t] && dp[i+t][j][k-t]); } } } } return dp[0][0][n];; } };
相关推荐
leetcode 答案 LeetCode-String #这是LeetCode里面String专题中的python版答案
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 ...
leetcode字符串转换为整数
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
Reverse words in a string-leetcode
大佬的leetcode刷题笔记(c++版本)
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
vs code LeetCode 插件
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
leetcode中文版
100个leetCode详细解答
LeetCode 刷题汇总1
LeetCode 选讲1
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
leetcode刷题, 直接用leetcode的分类方式.
leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 双指针 编号 题目 LeetCode 11 Container With Most Water LeetCode 19 Remove Nth Node From End of List LeetCode 42 ...
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
leetcode高频面试笔试题150+道,亲身总结。
LeetCode 刷题笔记