#131
Palindrome Partitioning
medium· Backtrackingruns: 0Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitions of s.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 742
class Solution:
def partition(self, s: str) -> list[list[str]]:
result = []
path = []
def is_palindrome(left: int, right: int) -> bool:
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
def backtrack(start: int) -> None:
if start == len(s):
result.append(path[:])
return
for end in range(start, len(s)):
if is_palindrome(start, end):
path.append(s[start : end + 1])
backtrack(end + 1)
path.pop()
backtrack(0)
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.