class Solution: # @param s, a string # @return a list of lists of string def isPalindrome(self, s): pattern = re.compile('[\W_]+') A=pattern.sub('', s.lower()) return A==A[::-1] def partition(self, s): n=len(s) rst=[] stack=[([],0)] ptable=[[False for i in range(n)] for j in range(n)] for i in range(n): for j in range(i+1,n+1): ptable[i][j-1]=self.isPalindrome(s[i:j]) i=0 while stack: crt=[] for i in stack: if i[1]==n: rst.append(i[0]) continue j=i[1]+1 while j<=n: if ptable[i[1]][j-1]==True: temp=i[0]+[s[i[1]:j]] crt.append((temp,j)) j+=1 stack=crt return rst
Friday, September 12, 2014
Leetcode: Palindrome Partitioning @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment