1
from functools import lru_cache5
def possiblyEquals(self, s1: str, s2: str) -> bool:7
def getValidPrefixLength(s, start):9
while end < len(s) and s[end].isdigit():14
def possibleLengths(s):15
"""Return all possible lengths represented by numeric string s."""17
for i in range(1, len(s)):18
# add all lengths by splitting numeric string s at i21
for x in possibleLengths(s[:i])22
for y in possibleLengths(s[i:])28
"""Return True if s1[i:] matches s2[j:] with given differences."""30
# If both have reached end return true if none of them are leading31
if i == len(s1) and j == len(s2):34
# s1 has not reached end and s1 starts with a digit35
if i < len(s1) and s1[i].isdigit():36
i2 = getValidPrefixLength(s1, i)37
for L in possibleLengths(s1[i:i2]):38
# substract since lead of s2 decreases by L39
if dp(i2, j, diff - L):42
# s2 has not reached end and s2 starts with a digit43
elif j < len(s2) and s2[j].isdigit():44
j2 = getValidPrefixLength(s2, j)45
for L in possibleLengths(s2[j:j2]):46
# add since lead of s2 increase by L47
if dp(i, j2, diff + L):50
# if none of them have integer prefix or a lead over the other52
# if only one of them has reached end or current alphabets are not the same53
if i == len(s1) or j == len(s2) or s1[i] != s2[j]:56
return dp(i + 1, j + 1, 0)58
# if none of them have integer prefix & s2 lead over s160
# no s1 to balance s2's lead63
# move s1 pointer forward and reduce diff64
return dp(i + 1, j, diff - 1)66
# if none of them have integer prefix & s1 lead over s268
# no s2 to balance s1's lead71
# move s2 pointer forward and increase diff72
return dp(i, j + 1, diff + 1)74
# start with starts of both s1 and s2 with no lead by any of them