Check if a Parentheses String Can Be Valid | Detailed Intuition | 2 Approaches | Leetcode 2116 | MIK

  Рет қаралды 9,154

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 141
@riteshhhhh_01
@riteshhhhh_01 2 күн бұрын
Tomorrow's quote from my side:- Don't have enough energy, felt tired, felt lazy, don't want to do it anymore, felt lonely, and on and onn.... Just one question) Who Cares..?
@suryapratap2219
@suryapratap2219 2 күн бұрын
accha nhi h
@Aryan-cy7cu
@Aryan-cy7cu Күн бұрын
some motivation from me : - No trees can grow to heaven ,unless it's roots reach down to hell.
@aryansinha1818
@aryansinha1818 Күн бұрын
bruhhh!! Damn it's so good
@AbhayGupta-m4v
@AbhayGupta-m4v Күн бұрын
What you are doing is speaking so loud that i cannot hear what you are saying
@jain5184
@jain5184 2 күн бұрын
Honestly, I used to be addicted to endlessly scrolling through KZbin and Instagram. It's been two months now since I stopped, and instead of wasting time on that, I've been focusing on solving daily problems, thanks to Mik Bhaiya. Thank you so much!
@codestorywithMIK
@codestorywithMIK 2 күн бұрын
That's amazing to hear! Keep up the great work! 💪😇
@jain5184
@jain5184 2 күн бұрын
@@codestorywithMIK thnku mik U helped a lot 🙏
@aws_handles
@aws_handles 2 күн бұрын
You are the only youtuber who seem to care more on video content, quality, information, knowledge instead of views,. Creating such detailed long videos take time for sure and you put so much effort every day. I respect you man. Thank you dil se.
@unknown47896
@unknown47896 Күн бұрын
yes bro...i agree with u
@chinmaygupta2277
@chinmaygupta2277 Күн бұрын
I have been following your channel for the last few months and I have achieved a consistency of 3 months now. Your explanation is just amazing. I have a request, can you please also share the leetcode weekly and biweekly contest solutions tutorial.
@codestorywithMIK
@codestorywithMIK Күн бұрын
Thanks for your feedback! I'll definitely keep that in mind for future videos. 🙏😇 Today’s contest Qn-3 was a good problem , i uploaded its video - kzbin.info/www/bejne/eGbEeqSCpppjlZosi=u0PYH6Alk51-d2_- Hope this helps ❤️🙏
@chinmaygupta2277
@chinmaygupta2277 Күн бұрын
@@codestorywithMIK Thankyou so much for considering my request. Yes I checked the third solution, amazing explanation once again.🥰
@Phobos221B
@Phobos221B Күн бұрын
These explanations are so good , people like you make teaching such a noble thing. Hats off!!!
@rickdutta942
@rickdutta942 Күн бұрын
East and West MIK is best. I haven't found anyone who put that much details and effort into explaining. Live longer man💖
@shivam-mp4vr
@shivam-mp4vr 2 күн бұрын
yaar laga aaj to fas hi gya din bana dete ho video se subah subah
@thefinancialdiet4458
@thefinancialdiet4458 Күн бұрын
Mik bhaiya want 2 things we the students are requesting u apka samjaya hua pura thought process dimag me jata hai even kabhi pehla consistent nhi tha apka subha motivation and then video thnku for all this but 1 apki sde sheet 2 dsa pattern recognition ispr video laao 3 for beginners to advance dsa course again ye app pr hai ki app launch karna chahta ho kuki honestly apka jesa ytber mene nhi dekha if u will for course jisme sabkuch included i will be infact bahut log course ke member hoga Again thanku if possible for u then it will be great help as u travel daily firbhi itna quality teaching content thought process salute u men 🫡
@mohammadaftabansari6882
@mohammadaftabansari6882 Күн бұрын
Thanks for such thorough explanations. Keep them coming.
@ujjwalsharma6773
@ujjwalsharma6773 2 күн бұрын
thank you bhaiya.. i was able to think of the second approach on the first try because of your older video intuitions, however was not able to think that i need to pass the string twice.. But the fact that i was even able to complete half of it feels really nice and its all because of you.. thank you so much
@Itisingh-mm8jd
@Itisingh-mm8jd Күн бұрын
Was really waiting for your explanation.. thanks 🎉 🙏
@aastikofficial6100
@aastikofficial6100 Күн бұрын
Sir just one question aap jb problem solve krte ho na to kafi acha thought process or say intution jo h na vo kafi achi build krte ho like kitna time lgega aapke jaisa sochne me like mne is question o 1st approach to khud kr li thi with same intution but that second one is damn good .... Now i recommend everyone to once checkout yout channel ... Thank you so much ❤❤..... Mostly yt videos just gave sol but you taught us thought process thats why i liked your videos 🔥
@RupaKumari-m3v5u
@RupaKumari-m3v5u Күн бұрын
you have a superpower to make tough problems easy.
@thefinalfit
@thefinalfit 2 күн бұрын
Finally 🎉 Thank you for posting even while travelling
@khushiiyer14
@khushiiyer14 Күн бұрын
Truely, a great explanation with complete patience 💯👏
@dayashankarlakhotia4943
@dayashankarlakhotia4943 2 күн бұрын
Motivation of day. 90%consistency will take you further then waiting for the 'perfect day',time or opportunity 🎉❤
@GauravDuseja-t6q
@GauravDuseja-t6q 19 сағат бұрын
Best Explanation thanku bhaiya
@wearevacationuncoverers
@wearevacationuncoverers 2 күн бұрын
Wow. You are the only one whom I can expect to bring detailed videos like these for intuition building. I already knew that we can solve it using 2-pass but needed few hints. got it now. thank you so much.
@NaveenKumar-do6cs
@NaveenKumar-do6cs Күн бұрын
Sir , the second approach was just fire.. that is really a smashing thought.
@сойка-и8й
@сойка-и8й Күн бұрын
Couldn't agree more, mind blowing solution
@chipmunk_god
@chipmunk_god Күн бұрын
MIK, your video on KMP was the best, the 1 hour was totally worth it, i really want you to make a similar video on Rolling hash, string hashing and rabin karp algorithm also with the same energy Love your work !!
@rockykumarverma980
@rockykumarverma980 2 күн бұрын
Thank you so much Bhaiya ji 🙏🙏🙏
@joydeepgharami403
@joydeepgharami403 2 күн бұрын
Thank you sir,1st comment
@pranavmaiya4386
@pranavmaiya4386 4 сағат бұрын
the second approach was really good
@PriyanshuSingh-qi3dk
@PriyanshuSingh-qi3dk 2 күн бұрын
2nd approach is really so good and easy you are great teacher bhaiya
@codestorywithMIK
@codestorywithMIK 2 күн бұрын
Glad you found the second approach helpful! 😊🙏
@сойка-и8й
@сойка-и8й Күн бұрын
​@@codestorywithMIKbhaiya Aapko ye second approach ka initiution kaise aaya... Ya phir ye koi Algorithm hai ?? Jaise fast slow pointer
@vyombajaj48
@vyombajaj48 2 күн бұрын
Your explanation is so good man
@codestorywithMIK
@codestorywithMIK 2 күн бұрын
I'm glad you found it helpful! 😇❤️🙏
@joydeep-halder
@joydeep-halder Күн бұрын
Bhaiyaaa kya tha vo approach 2 🤯🤯 Maaza aa gya
@SHUBHAMRAJPAL-w9m
@SHUBHAMRAJPAL-w9m Күн бұрын
Leetcode Problem 678. Valid Parenthesis String I guess this is the same problem that you are referring to in the video
@himanshujain9933
@himanshujain9933 Күн бұрын
Best Explanation Ever!
@aizad786iqbal
@aizad786iqbal 2 күн бұрын
hamare college mai kuch nahi padhaya gya tha dhang se..regarding DSA
@EB-ot8uu
@EB-ot8uu 2 күн бұрын
yayyy, MIK is here. I was stuck in one part. Now it's crystal clear and I solved it easily. Thank you so so much
@insanecode9374
@insanecode9374 Күн бұрын
amazing solution keep up bro !!
@bhupendrakalal1727
@bhupendrakalal1727 Күн бұрын
bhAIYA DIRECT optimal approach dimag me aa gayi thi , bs me ek hi bar me open aur close dono ka count store kr raha tha esiliye test case fat gaye , thank uu uuu
@codestorywithMIK
@codestorywithMIK Күн бұрын
Awesome 👌
@bhupendrakalal1727
@bhupendrakalal1727 Күн бұрын
@codestorywithMIK finally you replied🥳🥳🥳
@harjotanand834
@harjotanand834 2 күн бұрын
This video was " Are Bhai Bhai Bhai Bhai Bhai ....."😅😅
@TanmayMankar-26
@TanmayMankar-26 2 күн бұрын
In only one traversal we can check if our value doesn't go negative then at the end we have to check whether it is zero or not if it goes negative then it is false but otherwise the above condition
@sainipankaj
@sainipankaj 2 күн бұрын
Sir Keep doing good work
@harshtiwari416
@harshtiwari416 2 күн бұрын
Sir it would be helpful if you covered sorting algorithms by making a separate playlist :)
@CSBAjay
@CSBAjay 2 күн бұрын
Hi Mik , Can you cover contest problems too ? Your explanation is crystal clear 😊
@RitikaBaliyan-d7g
@RitikaBaliyan-d7g Күн бұрын
amazing
@tareqaziz3929
@tareqaziz3929 Күн бұрын
1614. Maximum Nesting Depth of the Parentheses to same
@ompandit4523
@ompandit4523 2 күн бұрын
thanks a lot bhaiya for those type of video
@gui-codes
@gui-codes 2 күн бұрын
Finally the legend is here. Was waiting for your exaplanation MIK.
@imPriyansh77
@imPriyansh77 2 күн бұрын
me too bro :)
@EB-ot8uu
@EB-ot8uu 2 күн бұрын
me too
@dss963
@dss963 Күн бұрын
This is extremely hard problem taking into account of edge cases.
@ShravanChoudhary-q1q
@ShravanChoudhary-q1q Күн бұрын
thankyou sirji..
@knight-z1x
@knight-z1x Күн бұрын
thankyou sir!!
@parthokr
@parthokr Күн бұрын
"Work in silence, let your success roar" reminds me something else.
@pradumyelame6462
@pradumyelame6462 Күн бұрын
Hello mik bhaiya I’m a third-year CSE student, and in six months, placements will begin at my college. I've been following your videos for the past 4 months, and they’ve been really helpful. However, I still feel like I’m struggling to solve DSA problems independently, especially when it comes to thinking through a problem to its end. I work on DSA daily, but I’m unsure what I might be missing in my preparation. Could you please guide me on how to improve my problem-solving skills and logical thinking so please bhaiya bata do kuch?...
@sahebraojadhav9727
@sahebraojadhav9727 2 күн бұрын
Thank you mik
@coldcoke9254
@coldcoke9254 Күн бұрын
Sir do you have any tips to build own testcases for questions like these. Since there were so many cases coming to my mind when solving these that I couldn't think of a algorithm to solve this. But when you used you own testcases to explain the intuition I was easily able to understand the solution. So can you give me tips to come up with such test cases on my own?
@subhranilnandy04
@subhranilnandy04 Күн бұрын
Consider the string, "( 0 0 0 ) )" [0 represents unlocked index]. Acc to your approach1, the 1st locked closing parenthesis (at idx 4) is matched with 1st opening locked parenthesis (idx 0), which is not the case here. Anyways, your approach passes all the testcases (along with this). So, I am a bit confused whether it's the correct approach to follow.
@manishv.8167
@manishv.8167 2 күн бұрын
Just a question why we need two traversal ?? Can't we do with one traversal for example you showed one where open = 2 from there we can return ans to be false, as we don't have matching parenthesis ???
@codestorywithMIK
@codestorywithMIK 2 күн бұрын
I was waiting if someone would ask this Qn. Glad you asked. Thank you. Let's see this scenario : s = ") ) ( ) ) )" l = "0 1 0 1 0 0" Left to Right, open = 2 if you check that since open > 0, we return false, that will be wrong. Because this string can be made balanced. It can be made balanced as - " ( ) ( ) ( )" I hope this example helps. But remember the WHY ? When traversing the string left to right, you maintain a count of open parentheses that represent the balance of the parentheses so far. This ensures that at no point do you have more closing ')' than open '(' up to that point. However, left-to-right traversal only ensures that every prefix of the string is valid. It does not guarantee that the entire string can be balanced, especially when there are unlocked 0s (flexible positions) that can later be adjusted. Right-to-left traversal ensures that every suffix of the string is also valid, which is equally important. When going right-to-left, we track close parentheses (instead of open) to check if there are enough ) to balance the ( from the right side.
@manishv.8167
@manishv.8167 2 күн бұрын
@codestorywithMIK awesome thanks
@amaannirban1107
@amaannirban1107 Күн бұрын
@@codestorywithMIK Would like to support Mazhar's response with example- s = "(()(" locked = "1101" right to left +2 but, ideally it should fail which will be catered in right to left
@gui-codes
@gui-codes 2 күн бұрын
14:02 - I paused the video and coded it within few minutes. Thank you so much MIK. One Qn, in interviews is it mandatory to solve using O(1) space ?
@imPriyansh77
@imPriyansh77 2 күн бұрын
Yes, if the interviewer demands space optimization.
@gui-codes
@gui-codes Күн бұрын
@@imPriyansh77 ok bhai
@gui-codes
@gui-codes 2 күн бұрын
Also, can you please post Qn-3 of today's contest. I have some doubts in it.
@sauravchandra10
@sauravchandra10 Күн бұрын
Really messed up the thinking. Had to see hints and arrive at this solution. Python implementation for the same: (btw the last approach was lit!) class Solution: def canBeValid(self, s: str, locked: str) -> bool: n = len(s) if n%2 != 0: return False lockedOpen, free = [], [] for i in range(n): if locked[i] == '0': free.append(i) else: if s[i] == ')': if len(lockedOpen) > 0: lockedOpen.pop() elif len(free) > 0: free.pop() else: return False else: lockedOpen.append(i) if len(lockedOpen) == 0 and len(free)%2 == 0: return True if len(lockedOpen) > len(free) or (len(free)-len(lockedOpen))%2 != 0: return False while lockedOpen: if lockedOpen[-1] > free[-1]: return False lockedOpen.pop() free.pop() return True
@harshugamer7776
@harshugamer7776 Күн бұрын
Second approach also can be done by single pass: class Solution { public boolean canBeValid(String s, String locked) { if(s.length() % 2 != 0) return false; int open = 0; int close = 0; for(int i = 0, j = s.length() - 1 ; i < s.length() ; i++, j--){ char ch1 = s.charAt(i); char ch2 = s.charAt(j); if(locked.charAt(i) == '0' || ch1 == '('){ open++; }else{ open--; } if(open < 0) return false; if(locked.charAt(j) == '0' || ch2 == ')'){ close++; }else{ close--; } if(close < 0) return false; } return true; } } this is single pass. right? or I have misunderstood?
@souravjoshi2293
@souravjoshi2293 2 күн бұрын
can you please post Qn-3 of today's contest. It was of graph Qn. Really thankful to you for providing such detailed videos everyday with consistency. You are my inspiration.
@harshitrajput3566
@harshitrajput3566 14 сағат бұрын
@@souravjoshi2293 bhai mei abhi new hu leetcode pr aap kab se leetcode problem solve kr rhe ho mujhe kuch tips chaiye thi bhai
@Heayu05
@Heayu05 Күн бұрын
Honestly, I have done 200+ qs in leetcode and now have the confidence to solve med level qs thanks to you 🙏, but I wanna ask is doing cp is good ? Many of my frnds are doing it as OA is generally getting tough !
@neerajgarg9096
@neerajgarg9096 2 күн бұрын
Java code : class Solution { public boolean canBeValid(String s, String locked) { int n = s.length(); if(n%2!=0){ return false; } int ob=0,cb=0,wc=0; for(int i=0; iob+wc){ return false; } } } ob=0; cb=0; wc=0; for(int i=n-1; i>=0; i--){ Character w = s.charAt(i); Character l = locked.charAt(i); if(l=='0'){ wc++; } else{ if(w=='('){ ob++; } else{ cb++; } if(ob>cb+wc){ return false; } } } return true; } }
@abhiramvanama5266
@abhiramvanama5266 2 күн бұрын
Sir can you please explain the 3rd graph question from the leetcode contest 432 please sir
@002_adityaagrawal2
@002_adityaagrawal2 Күн бұрын
Hello MIK, I have doubt on optimal solution you mentioned, 1)we will go from left to right than check if it will violate the condition or not, 2)than we will go right to left than check if it will violate the condition or not. I got the approach but when i am thinking of the solution in my mind came small doubt what would happen on first iteration we consider openclose parathesis as open parathesis now in case of second iteration we are considering as close parathesis. it would be very helpful if you consider my doubt. Thanks
@codestorywithMIK
@codestorywithMIK Күн бұрын
Glad you asked. One more guy asked this in the comment. Thank you for asking. Let's see this scenario : s = ") ) ( ) ) )" l = "0 1 0 1 0 0" Left to Right, open = 2 if you check that since open > 0, we return false, that will be wrong. Because this string can be made balanced. It can be made balanced as - " ( ) ( ) ( )" I hope this example helps. But remember the WHY ? When traversing the string left to right, you maintain a count of open parentheses that represent the balance of the parentheses so far. This ensures that at no point do you have more closing ')' than open '(' up to that point. However, left-to-right traversal only ensures that every prefix of the string is valid. It does not guarantee that the entire string can be balanced, especially when there are unlocked 0s (flexible positions) that can later be adjusted. Right-to-left traversal ensures that every suffix of the string is also valid, which is equally important. When going right-to-left, we track close parentheses (instead of open) to check if there are enough ) to balance the ( from the right side.
@002_adityaagrawal2
@002_adityaagrawal2 Күн бұрын
@@codestorywithMIK Thanks bhaiya, can we use second approach ,if we have to print balanced string?? As per my understanding we can not use.
@codestorywithMIK
@codestorywithMIK Күн бұрын
No printing a balanced string will not be a good idea with this approach. Stack is a better approach for that ❤️
@Sarthak2421
@Sarthak2421 2 күн бұрын
Hey Mazhar! Please make a video on 3418. Maximum Amount of Money Robot Can Earn , if possible .
@italk-gj5kk
@italk-gj5kk Күн бұрын
here is the java code: class Solution { public boolean canBeValid(String s, String locked) { Stack fixed = new Stack(), flexible = new Stack(); int n = s.length(); for(int i = 0; i < n; i++){ if(s.charAt(i) == ')' && locked.charAt(i) == '1'){ if(! fixed.empty()) fixed.pop(); else if(! flexible.empty()) flexible.pop(); else return false; } else{ if(locked.charAt(i) == '0') flexible.push(i); else fixed.push(i); } } while(! fixed.empty()) if(flexible.empty() || flexible.pop() < fixed.pop()) return false; return (n & 1) == 0; } }
@crazygamerrohan9899
@crazygamerrohan9899 2 күн бұрын
Not able to think 2nd part of logic 33:34
@indianengineer5802
@indianengineer5802 2 күн бұрын
two stack approach is extremely hard to think of!
@codestorywithMIK
@codestorywithMIK Күн бұрын
I totally agree. Actually i had solved similar problems where this 2 pass approach helps. That’s the reason i went for this approach as well. But I would like to give an example to share more on this approach. Let's see this scenario : s = ") ) ( ) ) )" l = "0 1 0 1 0 0" Left to Right, open = 2 if you check that since open > 0, we return false, that will be wrong. Because this string can be made balanced. It can be made balanced as - " ( ) ( ) ( )" I hope this example helps. But WHY does this work ? When traversing the string left to right, you maintain a count of open parentheses that represent the balance of the parentheses so far. This ensures that at no point do you have more closing ')' than open '(' up to that point. However, left-to-right traversal only ensures that every prefix of the string is valid. It does not guarantee that the entire string can be balanced, especially when there are unlocked 0s (flexible positions) that can later be adjusted. Right-to-left traversal ensures that every suffix of the string is also valid, which is equally important. When going right-to-left, we track close parentheses (instead of open) to check if there are enough ) to balance the ( from the right side.
@lofireverbz-wy7go
@lofireverbz-wy7go Күн бұрын
Little bit observation: same question as valid parenthesis string just consider 0 as *
@BalasrinivasCS
@BalasrinivasCS Күн бұрын
Sir want will happen if at the end there odd size of open close stack
@codestorywithMIK
@codestorywithMIK Күн бұрын
If open stack is not empty, we anyways return false irrespective of openClose i even or odd. But if open stack is empty, then openClose is guaranteed to be of even size. 21:25 I have explained why openClose will always be even in size if open stack is empty. Since openClose will always be even, hence no need to check. So only checking the open stack empty will do the job. Hope that helps ❤️😇🙏
@aryansinha1818
@aryansinha1818 Күн бұрын
25:00
@carsworld3433
@carsworld3433 Күн бұрын
hey bhaiya how can i think of important test cases for deciding the solution as such important test cases are not usually given in problem thank you
@BalasrinivasCS
@BalasrinivasCS Күн бұрын
Hello Sir, I have doubt about approach -1 when the open-close stack is even size at end can I return true ; you didn't mention that condition in the code
@codestorywithMIK
@codestorywithMIK Күн бұрын
I have explained the even size thing from 21:25 I have explained why openClose will always be even in size if open stack is empty. Since openClose will always be even, hence no need to check. So only checking the open stack empty will do the job. Hope that helps ❤️😇🙏
@thefinancialdiet4458
@thefinancialdiet4458 2 күн бұрын
Sir please give us DSA sheet for faang prep…thankyou so much for all these videos ❤❤❤❤
@codestorywithMIK
@codestorywithMIK 2 күн бұрын
I am in the process of completing the sheet. Will soon provide ❤️😇🙏
@thefinancialdiet4458
@thefinancialdiet4458 2 күн бұрын
@ sirrr pleaseeee provide usss soon….it will be a blessing for usss😭😭❤️❤️❤️
@thefinancialdiet4458
@thefinancialdiet4458 Күн бұрын
@@codestorywithMIK Mik bhaiya want 2 things we the students are requesting u apka samjaya hua pura thought process dimag me jata hai even kabhi pehla consistent nhi tha apka subha motivation and then video thnku for all this but 1 apki sde sheet 2 dsa pattern recognition ispr video laao 3 for beginners to advance dsa course again ye app pr hai ki app launch karna chahta ho kuki honestly apka jesa ytber mene nhi dekha if u will for course jisme sabkuch included i will be infact bahut log course ke member hoga Again thanku if possible for u then it will be great help as u travel daily firbhi itna quality teaching content thought process salute u men 🫡
@indianengineer5802
@indianengineer5802 2 күн бұрын
Hey MIK in case of simple parentheses check without that lock array. If we are doing forward and backward pass how we are identifying that with just count being positive there will be valid parentheses ? How count assures there is valid parentheses ? this part is confusing me
@codestorywithMIK
@codestorywithMIK Күн бұрын
I was waiting if someone would ask this Qn. Glad you asked. Thank you. Let's see this scenario : s = ") ) ( ) ) )" l = "0 1 0 1 0 0" Left to Right, open = 2 if you check that since open > 0, we return false, that will be wrong. Because this string can be made balanced. It can be made balanced as - " ( ) ( ) ( )" I hope this example helps. But remember the WHY ? When traversing the string left to right, you maintain a count of open parentheses that represent the balance of the parentheses so far. This ensures that at no point do you have more closing ')' than open '(' up to that point. However, left-to-right traversal only ensures that every prefix of the string is valid. It does not guarantee that the entire string can be balanced, especially when there are unlocked 0s (flexible positions) that can later be adjusted. Right-to-left traversal ensures that every suffix of the string is also valid, which is equally important. When going right-to-left, we track close parentheses (instead of open) to check if there are enough ) to balance the ( from the right side.
@divyanshu3404
@divyanshu3404 2 күн бұрын
if its 0 i will store both '(' and ')' in stack as pair or vector of char and will check with traditional method if anyone of it satisfy i will pop. Is this method correct?
@ShivenSingh-21
@ShivenSingh-21 Күн бұрын
Take an example ( ))) ->1010 in than case : index 0 will go to open bracket stack and index 1 will go to openClose bracket stack , so when we will encounter index 2 why will we pop out from open stack ,why not from openClose stack and if we are popping out from open bracket stack doesnt that make it illegal popping as you can only pop brackets adjacent to each other , how come close bracket at index 2 pops out locked open bracket at index 0 when you have 1 bracket in between ????
@vishwashsoni610
@vishwashsoni610 2 күн бұрын
sir solution code : class Solution { public: bool canBeValid(string s, string locked) { int n = s.size(); if(n%2 != 0){ return false; } stackopen; stackopen_close; for(int i=0;i open_close.top()){ return false; } else{ open.pop(); open_close.pop(); } } return open.empty(); } };
@adityaraj-zm7zk
@adityaraj-zm7zk Күн бұрын
please add the slides also in description
@bhuppidhamii
@bhuppidhamii 2 күн бұрын
1:15 😂
@VikasMishra-q9w
@VikasMishra-q9w Күн бұрын
Does anyone know where else we can apply that greedy approach with a constant space solution? That approach doesn’t seem intuitive at all
@codestorywithMIK
@codestorywithMIK Күн бұрын
Hi Vikas, usually in balanced parentheses related problems, this 2 way pass helps in some problems. Try Leetcode - 678 ❤️ Valid Parenthesis String | 4 Detailed Approaches | Leetcode 678 | codestorywithMIK kzbin.info/www/bejne/hp_SY5t3iciqfqM
@VikasMishra-q9w
@VikasMishra-q9w 17 сағат бұрын
@@codestorywithMIK Thanks Mik for clarification!
@aizad786iqbal
@aizad786iqbal 2 күн бұрын
still thora confusion hai, ki why we need right to left traversal, and why we can't check if open is 0 then return true else return false..
@codestorywithMIK
@codestorywithMIK Күн бұрын
I was waiting if someone would ask this Qn. Glad you asked. One more guy asked this in the comment. Thank you for asking. Let's see this scenario : s = ") ) ( ) ) )" l = "0 1 0 1 0 0" Left to Right, open = 2 if you check that since open > 0, we return false, that will be wrong. Because this string can be made balanced. It can be made balanced as - " ( ) ( ) ( )" I hope this example helps. But remember the WHY ? When traversing the string left to right, you maintain a count of open parentheses that represent the balance of the parentheses so far. This ensures that at no point do you have more closing ')' than open '(' up to that point. However, left-to-right traversal only ensures that every prefix of the string is valid. It does not guarantee that the entire string can be balanced, especially when there are unlocked 0s (flexible positions) that can later be adjusted. Right-to-left traversal ensures that every suffix of the string is also valid, which is equally important. When going right-to-left, we track close parentheses (instead of open) to check if there are enough ) to balance the ( from the right side.
@akarshthakur9839
@akarshthakur9839 Күн бұрын
Bhaiya go the second approach but wasnt able to get it like why is it working because esa to hai nhi ki yaa to hum sbko open hi maan lege yaa sbko closed hi maan lege to mixed lene pdege lekin phir bhi ye work ku kr rha hai if anyone can explain it will be really helpful
@codestorywithMIK
@codestorywithMIK Күн бұрын
Glad you asked. One more guy asked this in the comment. Thank you for asking. Let's see this scenario : s = ") ) ( ) ) )" l = "0 1 0 1 0 0" Left to Right, open = 2 if you check that since open > 0, we return false, that will be wrong. Because this string can be made balanced. It can be made balanced as - " ( ) ( ) ( )" I hope this example helps. But remember the WHY ? When traversing the string left to right, you maintain a count of open parentheses that represent the balance of the parentheses so far. This ensures that at no point do you have more closing ')' than open '(' up to that point. However, left-to-right traversal only ensures that every prefix of the string is valid. It does not guarantee that the entire string can be balanced, especially when there are unlocked 0s (flexible positions) that can later be adjusted. Right-to-left traversal ensures that every suffix of the string is also valid, which is equally important. When going right-to-left, we track close parentheses (instead of open) to check if there are enough ) to balance the ( from the right side.
@e_sumit
@e_sumit 2 күн бұрын
😉
@gamersgame43
@gamersgame43 Күн бұрын
I first tried this using bruteforce backtracking
@KishanSingh-vc3re
@KishanSingh-vc3re Күн бұрын
bhaiya contests k questions
@Faith_believe_0002
@Faith_believe_0002 2 күн бұрын
Sab ko batana zaroori hai aapne kiya acheive kiya hai ?
@akhilsharma392
@akhilsharma392 Күн бұрын
if(!open.empty() && !con.empty()){ if(open.top()>con.top()){ open.pop(); }else{ con.pop(); } } please explain while we are trying to pop for a closed one why we dont't use this condition
@priyankanagpal2207
@priyankanagpal2207 2 күн бұрын
How to think about all these logic?
@codestorywithMIK
@codestorywithMIK 2 күн бұрын
Hi there, at first it all seemed too tricky to me when I was studying all these. But after solving many such Qns, everything boils down to some pattern or generic observation. Same thing I mentioned in this video as well "---- When dealing with balanced parentheses problems, always think of using a stack. Stacks are perfect for matching opening and closing parentheses and ensuring the sequence is valid. ---- Additionally, remember that a two-way pass approach (left-to-right and right-to-left) is often useful in parentheses-related problems. It helps validate both prefixes and suffixes, ensuring global balance. This is especially helpful in problems with flexibility, like when certain characters can be treated as either ( or )." I hope this helps. Remeber, the key is solve as many Qns as possible. And whenever you observe some pattern or generic approach, always keep a note of it. because most of the time, such ways can help to solve other similar problems.
@imPriyansh77
@imPriyansh77 2 күн бұрын
@@codestorywithMIK Thank you bhaiya
@dayashankarlakhotia4943
@dayashankarlakhotia4943 2 күн бұрын
public boolean canBeEqual(String s,String locked){ int cmin=0,cmax=0; for(int i=0;i
@pratyushpandey1145
@pratyushpandey1145 2 күн бұрын
Sir i was not able to figure this question out on my own 😭😭😭
@gui-codes
@gui-codes 2 күн бұрын
same bhai. Mai bhi ek jagah stuck hogaya tha.
@wearevacationuncoverers
@wearevacationuncoverers 2 күн бұрын
thoda as hint maine bhi liya then solve hogaya tha mujhse but constant space wala abhi samajh aya finally. Don't worry bro, keep practicing.
@pratyushpandey1145
@pratyushpandey1145 2 күн бұрын
@@wearevacationuncoverers thnx 🙏🙏
@imPriyansh77
@imPriyansh77 2 күн бұрын
@@gui-codes Haa bhai mere bhi logic mai mistake thi
@jewelchakraborty9717
@jewelchakraborty9717 2 күн бұрын
Hi Mik bhai. we can do it in a single pass itself by tweaking the indexes. Please comment if you like this. /* My Java Code */ class Solution { public boolean canBeValid(String s, String locked) { int n = s.length(); if((n & 1) != 0) return false; int o = 0; int c = 0; for(int i = 0; i < n; i++){ if(s.charAt(i) == '(' || locked.charAt(i) == '0') { o++; } else{ o--; } if(o < 0) return false; if(s.charAt(n - i - 1) == ')' || locked.charAt(n - i - 1) == '0') { c++; } else{ c--; } if(c < 0) return false; } return true; } }
@codestorywithMIK
@codestorywithMIK 2 күн бұрын
Lovely ❤️
@AnirudhSingh-y8q
@AnirudhSingh-y8q Күн бұрын
Now a days you are doing very much travelling bhai ,why ?
@abhayshukla5175
@abhayshukla5175 2 күн бұрын
can anyone debug this why my code is failing only 202/258 case are passing not able to understand why not working class Solution { public: bool canBeValid(string s, string locked) { if(s.length()%2!=0) return false; stackst; bool ans=true; for(int i=0;i=unmatchedCount){ ans=true; } else ans=false; } return ans; } };
@yatri6329
@yatri6329 2 күн бұрын
Itna lamba video na banao bhai ..
@codestorywithMIK
@codestorywithMIK 2 күн бұрын
Apologies for the inconvenience. I have added pointers to the videos. So that those who are not beginners and don’t need to know entire detail can skip to main parts of the video using pointers. I usually detail more just for the sake of beginners who can benefit from the minute details. I really hope it will help. Again, apologies for the inconvenience. Hope you understand 😇❤️🙏
@wearevacationuncoverers
@wearevacationuncoverers 2 күн бұрын
@@codestorywithMIK MIK please keep videos detailed always. It helps those and specially me who are of beginner level and need minute details.
🎈🎈🎈😲 #tiktok #shorts
0:28
Byungari 병아리언니
Рет қаралды 4,5 МЛН
Counter-Strike 2 - Новый кс. Cтарый я
13:10
Marmok
Рет қаралды 2,8 МЛН
Pick these tech to make or destroy your tech career
15:52
Chai aur Code
Рет қаралды 134 М.
Reality of My 48 LPA Microsoft Salary after TAXES! #salary
11:17
Nishant Chahar
Рет қаралды 64 М.
BRAIN ROT | Why You Are Losing Control Of Your Brain?
17:40
Aevy TV
Рет қаралды 926 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 274 М.
are we cooked w/ o3?
13:58
ThePrimeTime
Рет қаралды 325 М.
My Outrageous Battle with the Lion Himself ft. Yograj Singh
1:09:37
UNFILTERED by Samdish
Рет қаралды 1,6 МЛН