Skip to content

Commit ba9c365

Browse files
authoredOct 11, 2020
Create split-two-strings-to-make-palindrome.py
1 parent aae3d08 commit ba9c365

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def checkPalindromeFormation(self, a, b):
6+
"""
7+
:type a: str
8+
:type b: str
9+
:rtype: bool
10+
"""
11+
def is_palindrome(s, i, j):
12+
while i < j:
13+
if s[i] != s[j]:
14+
return False
15+
i += 1
16+
j -= 1
17+
return True
18+
19+
def check(a, b):
20+
i, j = 0, len(b)-1
21+
while i < j:
22+
if a[i] != b[j]:
23+
return is_palindrome(a, i, j) or is_palindrome(b, i, j)
24+
i += 1
25+
j -= 1
26+
return True
27+
28+
return check(a, b) or check(b, a)

0 commit comments

Comments
 (0)
Please sign in to comment.