LeetCode Palindrome Partitioning Solved

laptop with a code solution of a palindrome partition problem

Description

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.

Solution

Code

def partition(s):
    if len(s) == 1:
        return [[s]]
    def backtrack(start, partition, result):
        if start == len(s):
            result.append(list(partition))
            return
        for i in range(start, len(s)):
            if s[start:i+1] == s[start:i+1][::-1]:
                partition.append(s[start:i+1])
                backtrack(i+1, partition, result)
                partition.pop()
    result = []
    backtrack(0, [], result)
    return result

s = "a"
print(partition(s))

Explanation

This code uses a backtracking approach to solve the problem. It starts with an empty partition, and for each substring of the given string “s”, it checks if the substring is a palindrome. If it is, it adds the substring to the partition and recursively calls the backtracking function with the next substring. If the entire string has been processed, it adds the partition to the result list. Finally, it returns the result list, containing all possible partitions of the given string “s” into palindromes.

List

list(partition) creates a new list with the elements of the partition variable. In python, list() is a constructor that creates a new list. It can be called with a single argument, which is an iterable (e.g. a string, tuple, or another list) and it will create a new list with the elements of the iterable in the same order.

In the code, partition is a list that stores the current partition of the input string “s” into palindromes. The backtrack function is called recursively and at each step, it adds a new palindrome substring to the partition list and then recursively calls itself with the next substring. When the entire string has been processed and a valid partition has been found, the function adds list(partition) to the result list.

What does s[::-1] mean?

In the code, s[::-1] is used to reverse the order of the characters in the string s. The slice notation s[start:end:step] is used to extract a substring from the string s. The start and end parameters are optional, and default to the beginning and end of the string respectively. The step parameter is also optional, and defaults to 1. When step is -1, it means to start at the end of the string and move backwards.

So s[::-1] returns a new string that has all the characters of the original string in reverse order.

What about [start:i+1][::-1]?

s[start:i+1][::-1] is used to reverse the order of the characters of substring of s from start to i+1.

  • s[start:i+1] is used to extract a substring from the string s starting from index start to i+1, it means it will return substring of s from index start to i.
  • [::-1] is used to reverse the order of the characters of the substring obtained from previous step.

So s[start:i+1][::-1] is used to check if the substring from start to i+1 is a palindrome or not by comparing it with its reverse, if it’s equal to the original substring then it’s a palindrome otherwise not.