-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dp_nm.py
41 lines (33 loc) · 987 Bytes
/
AC_dp_nm.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_dp_nm.py
# Create Date: 2015-03-19 09:45:12
# Usage: AC_dp_nm.py
# Descripton:
class Solution:
# @param s, a string
# @return a list of lists of string
def partition(self, s):
slen = len(s)
indexes = [[] for _ in range(slen)]
# pre calculate index
for i in range(slen):
for j in range(i, slen):
if s[i: j + 1] == s[i: j + 1][: : -1]:
indexes[i].append(j + 1)
ret = []
single = []
def find_partition(start):
for end in indexes[start]:
single.append(s[start: end])
if end == slen:
ret.append(single[:])
else:
find_partition(end)
single.pop()
find_partition(0)
return ret
# debug
s = Solution()
print s.partition('aab')