131_ Palindrome Partitioning
Last updated
Last updated
def partition(s):
"""
:type s: str
:rtype: List[List[str]]
"""
def helper(offset, partial):
# offset is the position of current string we are dealing with
# partial is the substring that are already partitioned
if offset == len(s):
output.append(list(partial)) # list is not neccesary here
return
# case when s[offset] is partitioned
helper(offset+1, partial+[s[offset]])
# find the next palindrome candidate
for i in range(offset+1, len(s)):
if s[i] == s[offset]:
if s[offset:i+1] == s[offset:i+1][::-1]:
helper(i+1, partial+[s[offset:i+1]])
output = []
helper(0, [])
return output