aboutsummaryrefslogtreecommitdiff
path: root/palindrome_partitioning.py
blob: 32805e8b842adf8390e1abd5e85b12a3efa3790a (plain)
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
# 131. Palindrome Partitioning
from collections import defaultdict

"""
given: string 's', parition 's' such that every substring of the parition
is a palindrome. return all possible palindrome paritioning of 's'.
"""


class Solution(object):
    def __init__(self):
        self.memory = defaultdict(list)

    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        """
        if not s:
            return [[]]
        if s in self.memory:
            return self.memory[s]
        ans = []
        for i in range(1, len(s) + 1):
            if s[:i] == s[:i][::-1]:
                for suf in self.partition(s[i:]):
                    ans.append([s[:i]] + suf)
        self.memory[s] = ans
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.partition(s="aab"))
    print(obj.partition(s="a"))