forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome_partitioning.py
60 lines (55 loc) · 1.73 KB
/
palindrome_partitioning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Author : Yagao0o
# Date : 2015-05-26
# Source : https://leetcode.com/problems/palindrome-partitioning/
# Given a string s, partition s such that every substring of the partition is a palindrome.
#
# Return all possible palindrome partitioning of s.
#
# For example, given s = "aab",
# Return
#
# [
# ["aa","b"],
# ["a","a","b"]
# ]
class Solution:
# @param s, a string
# @return a list of lists of string
def partition(self, s):
if len(s) == 0:
return []
result = []
#get all separate occasion
current = [[s[0]]]
for i in range(1,len(s)):
new = []
new_char = s[i]
for j in current:
first_case = j[:]
first_case.append(new_char)
new.append(first_case)
second_case = j[:]
new_last = second_case[len(second_case) - 1] + new_char
if self.is_palindrome(new_last):
second_case[-1] = new_last
new.append(second_case)
elif len(second_case) > 1 and second_case[-2] == new_char:
second_case[-2] = new_char + new_last
second_case.pop(-1)
new.append(second_case)
current = new
#remove those are not palindrome
for i in current:
is_ok = True
for j in i:
if not self.is_palindrome(j):
is_ok = False
break
if is_ok:
result.append(i)
return result
def is_palindrome(self, word):
for i in range(len(word)/2):
if word[i] != word[-(i+1)]:
return False
return True