Skip to content

Commit 38a603a

Browse files
committed
Runtime: 1026 ms (Top 83.52%) | Memory: 55.5 MB (Top 82.18%)
1 parent 6e342e4 commit 38a603a

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,63 @@
1+
// Runtime: 1026 ms (Top 83.52%) | Memory: 55.5 MB (Top 82.18%)
12
from functools import lru_cache
23
class Solution:
34
def possiblyEquals(self, s1: str, s2: str) -> bool:
4-
5+
56
def getValidPrefixLength(s,start):
67
end = start
78
while end < len(s) and s[end].isdigit(): end += 1
89
return end
9-
10+
1011
@lru_cache(None)
11-
def possibleLengths(s):
12+
def possibleLengths(s):
1213
"""Return all possible lengths represented by numeric string s."""
1314
ans = {int(s)}
1415
for i in range(1, len(s)):
1516
# add all lengths by splitting numeric string s at i
1617
ans |= {x+y for x in possibleLengths(s[:i]) for y in possibleLengths(s[i:])}
1718
return ans
18-
19+
1920
@lru_cache(None)
20-
def dp(i, j, diff):
21+
def dp(i, j, diff):
2122
"""Return True if s1[i:] matches s2[j:] with given differences."""
22-
23+
2324
# If both have reached end return true if none of them are leading
2425
if i == len(s1) and j == len(s2): return diff == 0
25-
26+
2627
# s1 has not reached end and s1 starts with a digit
27-
if i < len(s1) and s1[i].isdigit():
28+
if i < len(s1) and s1[i].isdigit():
2829
i2 = getValidPrefixLength(s1,i)
29-
for L in possibleLengths(s1[i:i2]):
30-
# substract since lead of s2 decreases by L
31-
if dp(i2, j, diff-L): return True
32-
30+
for L in possibleLengths(s1[i:i2]):
31+
# substract since lead of s2 decreases by L
32+
if dp(i2, j, diff-L): return True
33+
3334
# s2 has not reached end and s2 starts with a digit
34-
elif j < len(s2) and s2[j].isdigit():
35+
elif j < len(s2) and s2[j].isdigit():
3536
j2 = getValidPrefixLength(s2,j)
36-
for L in possibleLengths(s2[j:j2]):
37+
for L in possibleLengths(s2[j:j2]):
3738
# add since lead of s2 increase by L
38-
if dp(i, j2, diff+L): return True
39-
39+
if dp(i, j2, diff+L): return True
40+
4041
# if none of them have integer prefix or a lead over the other
41-
elif diff == 0:
42-
# if only one of them has reached end or current alphabets are not the same
43-
if i == len(s1) or j == len(s2) or s1[i] != s2[j]: return False
42+
elif diff == 0:
43+
# if only one of them has reached end or current alphabets are not the same
44+
if i == len(s1) or j == len(s2) or s1[i] != s2[j]: return False
4445
# skip same alphabets
4546
return dp(i+1, j+1, 0)
46-
47-
# if none of them have integer prefix & s2 lead over s1
48-
elif diff > 0:
47+
48+
# if none of them have integer prefix & s2 lead over s1
49+
elif diff > 0:
4950
# no s1 to balance s2's lead
5051
if i == len(s1): return False
5152
# move s1 pointer forward and reduce diff
5253
return dp(i+1, j, diff-1)
53-
54+
5455
# if none of them have integer prefix & s1 lead over s2
55-
else:
56+
else:
5657
# no s2 to balance s1's lead
57-
if j == len(s2): return False
58+
if j == len(s2): return False
5859
# move s2 pointer forward and increase diff
5960
return dp(i, j+1, diff+1)
60-
61+
6162
# start with starts of both s1 and s2 with no lead by any of them
62-
return dp(0, 0, 0)
63+
return dp(0, 0, 0)

0 commit comments

Comments
 (0)