随笔- 509  文章- 0  评论- 151  阅读- 22万 

Scramble String

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 };
复制代码

 

 posted on   zhuli19901106  阅读(362)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示