forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_dp_n^4.py
40 lines (33 loc) · 1.25 KB
/
AC_dp_n^4.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_dp_n^4.py
# Create Date: 2015-03-08 19:43:42
# Usage: AC_dp_n^4.py
# Descripton:
class Solution:
# @return a boolean
def isScramble(self, s1, s2):
if len(s1) != len(s2):
return False
if not len(s1):
return True
sz = len(s1)
dp = [[[False] * sz for _ in xrange(sz)] for _ in xrange(sz)]
for wd in xrange(0, sz): # the width of substring
for i in xrange(sz - wd): # start position of s1
for j in xrange(sz - wd): # start position of s2
if wd == 0:
dp[i][j][wd] = (s1[i] == s2[j])
else:
for cut in xrange(0, wd):
if dp[i][j][cut] and dp[i + cut + 1][j + cut + 1][wd - cut - 1]:
dp[i][j][wd] = True
break
if dp[i][j + wd - cut][cut] and dp[i + cut + 1][j][wd - cut - 1]:
dp[i][j][wd] = True
break
return dp[0][0][sz - 1]
# debug
s = Solution()
print s.isScramble('abb', 'bba')