Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions NeetCode/palindrome-partitioning/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
class Solution:
def partition(self, s: str) -> List[List[str]]:
res = []
partitions = []

def check_palindrome(s):
if not s:
return False

front = 0
back = len(s) - 1
while front <= back and s[front] == s[back]:
front += 1
back -= 1
return front > back

def append_palindrome(index, partition):
if index == len(s):
if not check_palindrome(partition):
return
res.append(partitions.copy() + [partition])
return

append_palindrome(index + 1, partition + s[index])

if check_palindrome(partition):
partitions.append(partition)
append_palindrome(index + 1, s[index])
partitions.pop()

append_palindrome(0, "")
return res

class Solution:
def partition(self, s: str) -> List[List[str]]:
res = []
partitions = []

def check_palindrome(s):
if not s:
return False

front = 0
back = len(s) - 1
while front <= back and s[front] == s[back]:
front += 1
back -= 1
return front > back

def append_palindrome(index, partition):
if index == len(s):
if not check_palindrome(partition):
return
res.append(partitions.copy() + [partition])
return

append_palindrome(index + 1, partition + s[index])

if check_palindrome(partition):
partitions.append(partition)
append_palindrome(index + 1, s[index])
partitions.pop()

append_palindrome(0, "")
return res

class Solution:
def partition(self, s: str) -> List[List[str]]:
res = []
partitions = []

def check_palindrome(s, left, right):
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True

def append_palindromes(index):
if index == len(s):
res.append(partitions.copy())
return

for j in range(index, len(s)):
if check_palindrome(s, index, j):
partitions.append(s[index : j + 1])
append_palindromes(j + 1)
partitions.pop()

append_palindromes(0)
return res
65 changes: 65 additions & 0 deletions NeetCode/palindrome-partitioning/step3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# time: O(2^n * n) generating all possible substrings, check if it is palindrome
# space: O(n)

class Solution:
def partition(self, s: str) -> List[List[str]]:
res = []
palindromes = []

def check_palindrome(s, left, right):
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True

def append_palindrome(index):
if index == len(s):
res.append(palindromes.copy())
return

for j in range(index, len(s)):
if check_palindrome(s, index, j):
palindromes.append(s[index : j + 1])
append_palindrome(j + 1)
palindromes.pop()

append_palindrome(0)
return res

class Solution:
def partition(self, s: str) -> List[List[str]]:
res = []
palindromes = []

def check_palindrome(s):
if not s:
return False

left = 0
right = len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True

def append_partitions(index, partition):
if index == len(s):
if not check_palindrome(partition):
return
res.append(palindromes.copy() + [partition])
return

partition += s[index]
append_partitions(index + 1, partition)

if check_palindrome(partition):
palindromes.append(partition)
append_partitions(index + 1, "")
palindromes.pop()

append_partitions(0, "")
return res