2014.2.27 00:04
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.
Solution:
First I considered DFS as a solution, but gave up quickly because it was too time-consuming.
Two strings s1 and s2 can be "scramble" only under either of the conditions below:
1. there exists a partition for s1 and s2, such that s1.left and s2.left are scramble, s1.right and s2.right are scramble.
2. there exists a partition for s1 and s2, such that s1.left and s2.right are scramble, s1.right and s2.left are scramble.
With this you can solve the problem with dynamic programming.
The DP array will require O(n^3) time and space, therefore the complexities are both O(n^3).
Accepted code:
1 // 2CE, 2RE, 1AC 2 class Solution { 3 public: 4 bool isScramble(string s1, string s2) { 5 int len1, len2; 6 7 len1 = (int)s1.length(); 8 len2 = (int)s2.length(); 9 // their lengths must be at least equal 10 if (len1 == 0 || len2 == 0 || len1 != len2) { 11 return false; 12 } 13 14 // they must at least anagrams 15 int c[256]; 16 int i, j, k, m; 17 for (i = 0; i < 256; ++i) { 18 c[i] = 0; 19 } 20 for (i = 0; i < len1; ++i) { 21 ++c[s1[i]]; 22 } 23 for (i = 0; i < len2; ++i) { 24 --c[s2[i]]; 25 } 26 for (i = 0; i < 256; ++i) { 27 if (c[i] != 0) { 28 return false; 29 } 30 } 31 32 int n = len1; 33 int ***dp; 34 dp = new int**[n]; 35 dp[0] = new int*[n * n]; 36 for (i = 1; i < n; ++i) { 37 dp[i] = &dp[0][0] + i * n; 38 } 39 dp[0][0] = new int[n * n * n]; 40 for (i = 1; i < n * n; ++i) { 41 dp[i / n][i % n] = &dp[0][0][0] + i * n; 42 } 43 44 for (i = 0; i < n; ++i) { 45 for (j = 0; j < n; ++j) { 46 for (k = 0; k < n; ++k) { 47 dp[i][j][k] = 0; 48 } 49 } 50 } 51 52 for (i = 0; i < n; ++i) { 53 for (j = 0; j < n; ++j) { 54 if (s1[i] == s2[j]) { 55 dp[0][i][j] = 1; 56 } 57 } 58 } 59 for (i = 1; i < n; ++i) { 60 for (j = 0; j + i < n; ++j) { 61 for (k = 0; k + i < n; ++k) { 62 for (m = 0; m < i; ++m) { 63 dp[i][j][k] = (dp[m][j][k] && dp[i - m - 1][j + m + 1][k + m + 1]) || 64 (dp[m][j][k + i - m] && dp[i - m - 1][j + m + 1][k]); 65 if (dp[i][j][k]) { 66 break; 67 } 68 } 69 } 70 } 71 } 72 int result = dp[n - 1][0][0]; 73 74 delete[] dp[0][0]; 75 dp[0][0] = nullptr; 76 delete[] dp[0]; 77 dp[0] = nullptr; 78 delete[] dp; 79 dp = nullptr; 80 81 return result == 1; 82 } 83 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)