Palindrome Partitioning - Backtracking - Leetcode 131

preview_player
Показать описание


0:00 - Read the problem
1:40 - Drawing Explanation
6:00 - Coding Explanation

leetcode 131

#palindrome #partition #python
Рекомендации по теме
Комментарии
Автор

You never jump to the solution directly that's what I like about this channel

arnabpersonal
Автор

Starting to see the template now for all these backtracking problems. You're great

woodylucas
Автор

This Solution can be optimized by using a memoization to store all the correct palindromes and the false ones so we only have to check the same substring in only O(1) time using a hashmap or a 2d array

peterlouis
Автор

Nice dude. Shortest and best explained solution I've encountered.

diegopena
Автор

Objection!! why is the time complexity is "2 over n". the decision tree has "width n" and "height n". it should "n over n"

yilmazbingol
Автор

You should use a cache for the palindrome function. Cache misses should try recursively do cache lookups for substrings too (i.e. start+1, end-1).

andrepinto
Автор

Hi, I have watched some of your videos and tried to solve this problem by myself this time, amazingly it works!, I can not believe I solve it in five minutes. it's another way so I put it here.
class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
# My solution
res=[]

def dfs(s, palin):
if len(s)==0:
res.append(palin[:])
else:
for i in range(len(s)):
if s[:i+1]==s[:i+1][::-1]:
palin.append(s[:i+1])
print(s[:i+1])
dfs(s[i+1:], palin)
palin.pop()

dfs(s, [])
return res

ruiqiliu
Автор

I spent a day thinking over this problem but couldn't think of the right solution.. it feels really annoying and almost feel like giving up.. don't think I can ever master this sorcery!

yossarian
Автор

I thought of sliding window initially but couldn’t solve entirely. Thanks for the explanation

dhruvalsharma
Автор

great explanation. thank you!! still having hard time wrapping my head around backtracking question ngl

mehmetnadi
Автор

I tried submitting a solution of mine on the NeetCode compiler, it did not accept the answer in different order. Since it is mentioned return in any order, all orders should have been accepted. Just thought I should share this feedback. Huge fan of your knowledge and all the help you are providing to us newbies!

mj_always
Автор

I initially started by finding all the possible palindrome substrings (i guess I need to pay more attention to the examples and instructions). The hardest part of this problem was understanding the desired output :p

KRD
Автор

Tricky question with wording, I originally thought it was saying it is a valid palindrome if letters can be rearranged to be a palindrome, but really it's the raw string being a palindrome.

Getting better at these though, backtracking definitely has a common format.

sucraloss
Автор

to check a palindrome string you can do something like: 'if s[i:j] == s[i:j][::-1]'

hanjiang
Автор

*Minor correction, the time complexity is not 2^n, it is n*2^n*

crazeeealgorithms
Автор

No one does it better than you man! Thanks a lot

ziadhassan
Автор

It's so hard to mentally walk through the exact path on these kinds of problems, with the falling through (and before that meeting conditions and not meeting conditions, entering and not entering new stacks and pushing and popping from shared references).

ianokay
Автор

hey could you make a solution for Palindrome Partitioning II as well(leetcode 132)? got stucked there for days due to TLE. your video is always the most helpful one :)

dinzonnyuoe
Автор

Returns [] instead of the expected output, checked it several times

bandanajha
Автор

Clean and clear expalantion!
Q: Why time complexity is 2^n? I just failed to figure it out

juliahuanlingtong