Given a string s
, partition s
such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s
.
Input: s = "aab" Output: [["a","a","b"],["aa","b"]]
Input: s = "a" Output: [["a"]]
1 <= s.length <= 16
s
contains only lowercase English letters.
from functools import lru_cache
class Solution:
@lru_cache()
def partition(self, s: str) -> List[List[str]]:
if len(s) == 0:
return [[]]
ret = []
for i in range(1, len(s) + 1):
if s[:i] == s[:i][::-1]:
ret.extend([[s[:i]] + x for x in self.partition(s[i:])])
return ret