-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path87_ScrambleString.py
71 lines (60 loc) · 2.05 KB
/
87_ScrambleString.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def isScramble(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
if (len(s1) != len(s2)) or not len(s1) or not len(s2):
return False
if s1 == s2:
return True
str_l = len(s1)
# dp[l][i][j]: whether s1[i:i+l+1] is a scrambled string of s2[j:j+l+1]
dp = [[[False for i in xrange(str_l)]
for j in xrange(str_l)] for l in xrange(str_l)]
# Initialization: dp[0][i][j], s1[i] is a scrambled string of s2[j]
for i in xrange(str_l):
for j in xrange(str_l):
dp[0][i][j] = True if s1[i] == s2[j] else False
for l in xrange(1, str_l):
# The length of current substring is l+1
for i in xrange(str_l-l):
for j in xrange(str_l-l):
# Split the l+1 string into two parts,
# k is the length of first part, so 1 <= k <= l;
for k in range(1, l+1):
scramble_1 = dp[k-1][i][j] and dp[l-k][k+i][k+j]
scramble_2 = dp[k-1][i][j+l-k+1] and dp[l-k][i+k][j]
dp[l][i][j] = (scramble_1 or scramble_2)
if dp[l][i][j]:
break
return dp[str_l-1][0][0]
"""
"great"
"rgeta"
"great"
"rgtae"
"""
# Implement with recursion
"""
class Solution(object):
def isScramble(self, s1, s2):
if (len(s1) != len(s2)) or not len(s1) or not len(s2):
return False
if sorted(s1) != sorted(s2):
return False
if s1 == s2:
return True
length = len(s1)
for i in range(1, length):
if (self.isScramble(s1[:i],s2[:i])
and self.isScramble(s1[i:],s2[i:])):
return True
if (self.isScramble(s1[:i],s2[length-i:])
and self.isScramble(s1[i:],s2[:length-i])):
return True
return False
"""