Reverse Substrings Between Each Pair of Parentheses - Leetcode 1190 - Python

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


0:00 - Read the problem
0:47 - Drawing Explanation 1
8:02 - Coding Explanation 1
10:58 - Drawing Explanation 2
18:37 - Coding Explanation 2

leetcode 1190

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

1:30 NeetCode loves us all, confirmed.

juanmacias
Автор

I actually thought the optimal sol but wasnt confident if i will code that properly. Nice Explanation NEET

AkeshKumar-hg
Автор

Dude, i would have never come up with that second solution in my wildest dreams

harshkhandelwal
Автор

I was having a hard time understanding the O(n) time approach. You explained it so well. Thank you !

MP-nyep
Автор

Thanks for the awesome explanation
I was very close to coming up with the linear time solution on my first try, But I got stuck on the two indexes approach until I ran out of time for solving the question. I then found your video online and realized how close I was if only I thought of creating a pair using a stack rather than stick to the two pointers approach.

Thanks for the informative and amazing video NeetCode!

EbrahemDroobi
Автор

Thanks for the second solution Similar to you I also got first one crt !

coolsai
Автор

Great video! Are there other leetcode problems that we can apply the second wormhole solution?

shibainu
Автор

The linear solution literally blew my mind....I think the harder part is to turn observation into an algorithm because I noticed the alternating pattern but I could not deduce how one pointer could just write the solution if you give it the right pivot points and jumps

ABC-opnz
Автор

Nah im happy with my n2 solution, never could have come up with that linear solution

tuandino
Автор

the man who came wtih approach two he is a genius Wooow.Kudos

UzairAhmed-gnsu
Автор

thanks for that great video, I have one question which is should I solve leetcode problems after finishing each topic (I mean solving problems on each specific topic) or should I wait until I finish the full DSA roadmap of whatever course I'm taking then start solving problems on the full DSA?

ahmedelhadary
Автор

SIMPLE CODE:
With same intuition, since the characters in even numbered paranthesis will not get reversed and the characters in odd numbered will get reversed.
We can simply use 2 pointers (start=0 and end=N-1) for the result array which will have same size as original array. Then do the following -
1. Iterate the original array using "current" pointer starting from index 0.
2. Also maintain an integer variable "count" which will keep track of the count of paranthesis in which our "current" pointer lies. If we encounter '(' then do ++count, and if we encounter ')' then do --count.
2. If current character lies in even numbered paranthesis (i.e. when "count" is even) then write that character at "start" position in result array and move "start" to next position.
3. If current character lies in odd numbered paranthesis (i.e. when "count" is odd) then write that character at "end" position in result array and move "end" one position backward.

deepaksingh
Автор

Although I came up with O(n) solution initially and failed to really code it up. I am settling for O(n^2) solution for being intuitive and that what I can actually code up during interview.

freecourseplatformenglish
Автор

Really cool solution, the second one!

wonderweebwoman
Автор

We need Dice roll simulation problem (Hard).

Igoy_
Автор

I had solved the problem in the same way as the 1st approach
but it didn't beat that many people 😅

PreethamC-wp
Автор

I can't imagine, I actually though of the same 2nd method just as he said after banging my head for 40 mins

shridhanushreddydappili
Автор

kinda had the intution for second algo but could not code

sojwalgosavi
Автор

I have sent you a LinkedIn request too.

jagrutitiwari
Автор

I got the intuition on O(n) but I couldn't able to code it up... I don't know where I'm lacking in...

MohanRam-mqpk