-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path214.py
23 lines (23 loc) · 978 Bytes
/
214.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
__________________________________________________________________________________________________
sample 32 ms submission
class Solution:
def shortestPalindrome(self, s):
if not s or len(s) == 1: return s
j = 0
for i in reversed(range(len(s))):
if s[i] == s[j]:
j += 1
return s[j:][::-1] + self.shortestPalindrome(s[:j-len(s)]) + s[j-len(s):]
__________________________________________________________________________________________________
sample 13232 kb submission
class Solution:
def shortestPalindrome(self, s: str) -> str:
#print(s)
if not s or len(s) == 1:
return s
j = 0
for i in reversed(range(len(s))):
if s[i] == s[j]:
j += 1
return s[::-1][:len(s)-j] + self.shortestPalindrome(s[:j-len(s)]) + s[j-len(s):]
__________________________________________________________________________________________________