動機
不同的DP不同的難易度
Problem
We can scramble a string s to get a string t using the following algorithm:
- If the length of the string is 1, stop.
- If the length of the string is > 1, do the following:
- Split the string into two non-empty substrings at a random index, i.e., if the string is
s
, divide it tox
andy
wheres = x + y
. - Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step,
s
may becomes = x + y
ors = y + x
. - Apply step 1 recursively on each of the two substrings
x
andy
.
- Split the string into two non-empty substrings at a random index, i.e., if the string is
Given two strings s1
and s2
of the same length, return true
if s2
is a scrambled string of s1
, otherwise, return false
.
Example 1:
Input: s1 = great, s2 = rgeatOutput: trueExplanation: One possible scenario applied on s1 is:great --> gr/eat // divide at random index.gr/eat --> gr/eat // random decision is not to swap the two substrings and keep them in order.gr/eat --> g/r / e/at // apply the same algorithm recursively on both substrings. divide at ranom index each of them.g/r / e/at --> r/g / e/at // random decision was to swap the first substring and to keep the second substring in the same order.r/g / e/at --> r/g / e/ a/t // again apply the algorithm recursively, divide at to a/t.r/g / e/ a/t --> r/g / e/ a/t // random decision is to keep both substrings in the same order.The algorithm stops now and the result string is rgeat which is s2.As there is one possible scenario that led s1 to be scrambled to s2, we return true.
Example 2:
Input: s1 = abcde, s2 = caebdOutput: false
Example 3:
Input: s1 = a, s2 = aOutput: true
Constraints:
s1.length == s2.length
1 <= s1.length <= 30
s1
ands2
consist of lower-case English letters.
Ver1:WA
def allOcuurLenFromEnd(s,c,i,j):
#print('find',c,i,j)
ans = []
while i < j:
i = s.find(c,i,j+1)
if i != -1:
ans.append(j-i+1)
i += 1
else:
break
return ans
dp = {}
def f(s1,s2,i,j,l):
#print(i,j,l)
if (i,j,l) not in dp:
if l is 0:
dp[(i,j,l)] = True
elif l is 1:
dp[(i,j,l)] = s1[i] == s2[j]
else:
if s1[i] == s2[j]:
dp[(i,j,l)] = f(s1,s2,i-1,j-1,l-1)
else:
ii = allOcuurLenFromEnd(s2,s1[i],j+1-l,j)
#print(ii)
for x in ii:
ret = f(s1,s2,i-x,j-x,l-x) and f(s1,s2,i-1,j,x-1)
#print("ret",ret,(i-x,j-x,l-x),(i-1,j,x-1))
if ret:
dp[(i,j,l)] = ret
return ret
dp[(i,j,l)] = False
#print((i,j,l),dp[(i,j,l)])
return dp[(i,j,l)]
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
dp.clear()
return f(s1,s2,len(s1)-1,len(s2)-1,len(s1))
Ver2: WA
def allOcuurLenFromEnd(s,c,i,j):
#print('find',c,i,j)
ans = []
while i < j:
i = s.find(c,i,j+1)
if i != -1:
ans.append(j-i+1)
i += 1
else:
break
return ans
def getll(s1,s2,i,j,limit):
ll = 0
while 0 <= i and 0 <= j and s1[i] == s2[j] and limit > 0:
ll += 1
i -= 1
j -= 1
limit -= 1
return ll
dp = {}
def f(s1,s2,i,j,l):
print(i,j,l)
if (i,j,l) not in dp:
if l is 0:
dp[(i,j,l)] = True
elif l is 1:
dp[(i,j,l)] = s1[i] == s2[j]
else:
if s1[i] == s2[j]:
dp[(i,j,l)] = f(s1,s2,i-1,j-1,l-1)
else:
s2_start_index = j+1-l
s2_end_index = j
lens = allOcuurLenFromEnd(s2,s1[i],s2_start_index,s2_end_index)
print("lens",lens)
for x in lens:
occur_in_s2 = j-x+1
pos_to_find_ll_in_s2_from = occur_in_s2-1
pos_to_find_ll_in_s1_from = i-1
ll = getll(s1,s2,pos_to_find_ll_in_s1_from,pos_to_find_ll_in_s2_from,l-1)
lhs_len = l-x-ll
print("ll",ll)
lhs_s1_end = i-x-ll
lhs_s2_end = j-x-ll
rhs_len = x - 1
rhs_s1_end = i-1-ll
rhs_s2_end = j
ret = f(s1,s2,lhs_s1_end,lhs_s2_end,lhs_len) and f(s1,s2,rhs_s1_end,rhs_s2_end,rhs_len)
print("ret",ret,(i-x,j-x,l-x),(i-1,j,x-1))
if ret:
dp[(i,j,l)] = ret
return ret
dp[(i,j,l)] = False
print((i,j,l),dp[(i,j,l)])
return dp[(i,j,l)]
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
dp.clear()
return f(s1,s2,len(s1)-1,len(s2)-1,len(s1))
Ver2: AC
case 有點多
- 交叉
- 直的
- 前或後有一樣的字
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
dp = {}
if len(s1) != len(s2):
return False
#s1, s2 = removePrefix_postfix(s1,s2)
#if not s1 and not s2:
# return True
for i in range(0,len(s1)):
for j in range(0,len(s1)):
dp[(i,j,1)] = s1[i] == s2[j]
for l in range(2,len(s1)+1):
for i in range(0,len(s1)):
for j in range(0,len(s1)):
if i-l < 0 or j-l < 0:
dp[(i,j,l)] = False
for l in range(2,len(s1)+1):
for x in range(1,l):
for i in range(1,len(s1)):
if i-x >= 0:
for j in range(1,len(s1)):
if j-(l-x) >= 0:
if (i,j,l) not in dp:
dp[(i,j,l)] = False
dp[(i,j,l)] = dp[(i,j,l)] or (i-l+1,j-l+1,1) in dp and dp[(i-l+1,j-l+1,1)] and dp[(i,j,l-1)]
dp[(i,j,l)] = dp[(i,j,l)] or (dp[(i,j-(l-x),x)] and dp[(i-x,j,l-x)]) or (dp[(i,j,1)] and dp[(i-1,j-1,l-1)]) or (i-(l-x) >= 0 and j-(l-x) >= 0 and dp[(i,j,(l-x))] and dp[(i-(l-x),j-(l-x),x)])
#print((i,j,l),dp[(i,j,l)],(i,j-(l-x),x), dp[(i,j-(l-x),x)] ,(i-x,j,l-x), dp[(i-x,j,l-x)])
return dp[(len(s1)-1,len(s2)-1,len(s1))]
Sol
照題目走
def cache_computing(f,is_computing=False):
mem = {}
@functools.wraps(f)
def wrapper(*args,**kwds):
if args not in mem:
mem[args] = is_computing
mem[args] = f(*args,**kwds)
return mem[args]
return wrapper
class Solution:
@cache_computing
def isScramble(self, s1: str, s2: str) -> bool:
#print(s1,s2)
if not s1 and not s2:
return True
elif s1[0] == s2[0]:
return self.isScramble(s1[1:],s2[1:])
elif len(s1) == 1:
return False
else:
return any([(self.isScramble(s1[k:],s2[k:]) and self.isScramble(s1[:k],s2[:k])) or (self.isScramble(s1[k:],s2[:len(s2)-k]) and self.isScramble(s1[:k],s2[len(s2)-k:])) for k in range(1,len(s1))])