LeetCode 131 Palindrome Partitioning

LeetCode 131 Palindrome Partitioning Problem

Download Code
class Solution(object):
    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        """
        # https://discuss.leetcode.com/topic/6186/java-backtracking-solution/2
        result = []
        curr = []
        self.recurPartition(result, curr, s, 0)
        return result

    def recurPartition(self, result, curr, s, start):
        if start == len(s):
            result.append(list(curr))
        for i in range(start, len(s)):
            if self.isPalindrome(s, start, i):
                curr.append(s[start:i + 1])
                self.recurPartition(result, curr, s, i + 1)
                curr.pop()

    def isPalindrome(self, s, begin, end):
        while begin < end:
            if s[begin] != s[end]:
                return False
            begin += 1
            end -= 1
        return True
Download Palindrome Partitioning.py

List of all Palindrome Partitioning problems

Leetcode 131 Palindrome Partitioning problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.