1
# Runtime: 271 ms (Top 15.83%) | Memory: 15.4 MB (Top 41.73%)
2
class Solution:
3
def checkPalindromeFormation(self, a: str, b: str) -> bool:
4
def pal(x):
5
return x == x[::-1]
6

7
if pal(a) or pal(b):
8
return True
9
# either grow from inside to outside, or vice versa
10
ina = len(a) - 1
11
inb = 0
12
outa = 0
13
outb = len(b) - 1
14

15
while a[ina] == b[inb]:
16
ina -= 1
17
inb += 1
18
if ina <= inb:
19
return True # short circuit found break point
20
# jump into each string now!?
21
# is a or b a palindrome in this portion from inb to ina
22
if pal(a[inb : ina + 1]) or pal(b[inb : ina + 1]):
23
return True # either one is breakpoint, so check remainder is palindrome
24

25
while a[outa] == b[outb]:
26
outa += 1
27
outb -= 1
28
if outa >= outb:
29
return True
30
if pal(a[outa : outb + 1]) or pal(b[outa : outb + 1]):
31
return True # either one is breakpoint, so check remainder
32

33
return False

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0