Skip to content

Commit cefe01c

Browse files
committed
leetcode.com 131. Palindrome Partitioning
문제 링크: https://leetcode.com/problems/palindrome-partitioning/description/
1 parent 62450e1 commit cefe01c

File tree

1 file changed

+32
-0
lines changed
  • leetcode.com 131. Palindrome Partitioning

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def partition(self, s: str) -> List[List[str]]:
6+
answer = []
7+
self.bt(s, 0, [], answer)
8+
9+
return answer
10+
11+
def bt(self, rest, idx_left, pathes, answer):
12+
if len(rest) == 0:
13+
answer.append(pathes)
14+
return
15+
16+
for idx_right in range(idx_left, len(rest)):
17+
part = rest[idx_left:idx_right + 1]
18+
if self.is_palindrome(part):
19+
self.bt(rest[idx_right + 1:], 0, pathes + [part], answer)
20+
21+
def is_palindrome(self, s):
22+
idx_left, idx_right = 0, len(s) - 1
23+
24+
while (idx_left <= idx_right):
25+
if s[idx_left] == s[idx_right]:
26+
idx_left += 1
27+
idx_right -= 1
28+
continue
29+
else:
30+
return False
31+
32+
return True

0 commit comments

Comments
 (0)