Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 893 Bytes

File metadata and controls

40 lines (30 loc) · 893 Bytes

131. Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.

Example 1:

Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]

Example 2:

Input: s = "a"
Output: [["a"]]

Constraints:

  • 1 <= s.length <= 16
  • s contains only lowercase English letters.

Solutions (Python)

1. Solution

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