GENIUS. Better than all solutions on leetcode, especially the one posted by them.
@techdose4u2 жыл бұрын
Thanks ☺️
@gautamthakur85818 ай бұрын
Its been 3 years of posting the solution ,, still it is the best
@meetnikhil7194 жыл бұрын
That moment when I heard the position of the * is important. Hats off!!
@techdose4u4 жыл бұрын
Thanks :)
@meetnikhil7194 жыл бұрын
@@techdose4u no, Thank you!!
@rishabhkumar80298 ай бұрын
Really very good explanation
@ankurgupta46964 жыл бұрын
The moment you said that positions are important i understood the concept and coded your concept without even watching full video thanks for your awesome vidoes.....
@techdose4u4 жыл бұрын
😂Nice :)
@akshatjain68544 жыл бұрын
Thanks Bro. I was able to pass 28 test cases out of 50 in Leetcode . The main thing which was frustrating me was how to deal with positions. You made it very simple .
@techdose4u4 жыл бұрын
I am happy that you tried to solve yourself :)
@abishekbaiju17056 ай бұрын
After a lot of time I kind of came close on how to solve this. I tried solutions from neetcode, leetcode. but none of them were making sense. but your solution was so so clear. Thanks a lot. I was able to code the solution by myself.
@techdose4u6 ай бұрын
Nice :)
@prajwal96104 жыл бұрын
That was an excellent explanation of this problem. Thanks for giving examples for all the cases.
@techdose4u4 жыл бұрын
Thanks :)
@traton30634 жыл бұрын
thanks for another concise and insightful video. I appreciate the fact that you carefully went through the questions and explain the approach with clear details. Also, quickly go through the code like what you did is perfect because the key is the approach and the algorithm for the problem. Great work! Looking forward to seeing more videos from you buddy!
@techdose4u4 жыл бұрын
Thanks :)
@sridharbattala30772 жыл бұрын
This is awesome explanation, even leetcode also doesn't have explanation like this
@uthrakaruna9998 ай бұрын
it's a great explanation and solution , here is the same approach in python (if any one needs) class Solution: def checkValidString(self, s: str) -> bool: opens=[] stars=[] for i in range(len(s)): if s[i]=="(": opens.append(i) elif s[i]==")": if opens: opens.pop() elif stars: stars.pop() else: return False else: stars.append(i) # if open is not empty then it is not balanced if opens: for i in range(len(opens)-1,-1,-1): if not stars or stars.pop()
@yitingg79423 жыл бұрын
Yeah!!! Got another right following your video Sir. You just make everything that had been haunting me to go away so easily. Thank god that you are here with me!!
@techdose4u3 жыл бұрын
❤️ Keep going
@molyoxide8358 Жыл бұрын
You've made this question clear with enough examples than leetcode. thanks for that.
@praveengautam46892 жыл бұрын
genius approach of using stacks
@ehsona18274 жыл бұрын
explanation is top notch, I have solved a similar problem to this problem. The best solution I've come up with is by doing 2 linear passes from left side and then from right side. O(n) time, O(1) space.
@techdose4u4 жыл бұрын
Nice....can you please share your code here?
@surbhi1914 жыл бұрын
@@techdose4u int count = 0; for(int i = 0; i=0; i--){ char c = s.charAt(i); if(c==')' || c=='*') count++; else{ if(count==0) return false; count--; } } return true;
@nandpatel9244 жыл бұрын
100 percent faster, my god i was shocked , really shocked
@techdose4u4 жыл бұрын
😅
@vani39868 ай бұрын
Great explanation with best solution
@stith_pragya Жыл бұрын
Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@bfoot239 Жыл бұрын
Great job man! This solution is much clear and even better for me to understand the problem than the leetcode editorial solutions
@thakuranujbhati15658 ай бұрын
Such a master of explanation ❤❤ that any one can easily understand the problem
@crimsoncad32304 жыл бұрын
1 optimization can be done here. After processing closing brackets if len(openStack) > len(star): Return False Else Process openStack from right to left
@techdose4u4 жыл бұрын
I think finding the length will again take O(N) if starts counting the element. But if counting takes O(1) than its good. Glad that you noticed. I saw it while editing 😅
@crimsoncad32304 жыл бұрын
@@techdose4uDon't know about other languages but in Python the time complexity to find the length of a list(in this case stack) is O(1). So this optimization can be beneficial.
@crimsoncad32304 жыл бұрын
@@techdose4u We can also have a counter that manages the count of element in the stack after every push and pop operation.
@techdose4u4 жыл бұрын
Yea.....it will be great :)
@techdose4u4 жыл бұрын
This sounds better :) Works for all languages ;)
@RV-qf1iz2 жыл бұрын
7:56 new word in the dictionary. Well Explained
@chenghuilee69824 жыл бұрын
Thank you sir! Very detailed explanation. You deserve more subs .
@techdose4u4 жыл бұрын
Thanks :)
@TheLaidia3 жыл бұрын
THANK YOU!! You make this problem a lot easier
@techdose4u3 жыл бұрын
Welcome
@azeezsayyad72814 жыл бұрын
thanks bro u deserve lot more subscribers
@techdose4u4 жыл бұрын
Thanks bro :)
@hortsss4 жыл бұрын
Thanks for your explanation! At first I didn't understand every possibilities the '*' had. And also, would never think about doing it using stacks. Congrats, man!
@techdose4u4 жыл бұрын
Thanks :)
@anshumansinghrajawat72423 жыл бұрын
Nice explanation brother , keep uploading leetcode videos as much as possible
@darkexodus6404 Жыл бұрын
Very articulately explained! Thank you!!
@pranayraj79384 жыл бұрын
Such a good explanation......you deserve more subscribers bro.
@techdose4u4 жыл бұрын
Thanks :)
@chandrasekharpatra24164 жыл бұрын
Thank you finally someone explained it better.
@techdose4u4 жыл бұрын
Welcome :)
@tumul14744 жыл бұрын
awesome dude !! you make amazing tutorials
@techdose4u4 жыл бұрын
Thanks :)
@nabinkumarshaw44748 ай бұрын
easy and better nice explaination
@amiransari19744 жыл бұрын
this video is very much helpful to understand the problem, Thanks
@techdose4u4 жыл бұрын
Welcome :)
@subhabera57754 жыл бұрын
Very good explanation.. earned my subscription
@techdose4u4 жыл бұрын
Thanks :)
@retrogame31384 жыл бұрын
Very nice you regularly upload the video soon you have more than 100 k subscriber
@techdose4u4 жыл бұрын
Hope it comes true someday 😅
@ajy78678 ай бұрын
Loved your explanation
@RaviYadav-xg2hy4 жыл бұрын
Great explanation and approach made it so easy problem !!
@techdose4u4 жыл бұрын
Thanks :)
@ris_colid325 Жыл бұрын
Any intuition for the greedy approach?
@ianpan01024 жыл бұрын
Great detailed solution! Except I have one minor issue: From around 5:00 - 5:20, we are trying to balance out the last closing parenthesis at position 6. We see that open_stack is empty, so we aim for the star_stack. According to your explanation, we would pop the star at index 4 to be converted to an open parenthesis and match it with closing parenthesis #6. But intuitively when looking at the string, we should have used the star at index 0 as an open parenthesis for closing paren #6 -- because the star at index 4 should really be a opening match for the closing parenthesis at index 5. Doesn't this matter?
@techdose4u4 жыл бұрын
Actually think the other way. I said the last star is popped to match current closing bracket because you convert last star to opening bracket and ignore other brackets, the entire parenthis gets balanced. I said that meaning to take care of current imbalance due to an extra closing bracket. This doesn't mean that inner bracket will balance outer bracket. I hope you got it :)
@ianpan01024 жыл бұрын
@@techdose4u I think I got it. Btw, I came across another method that keeps track of the balance and loop through left to right then right to left. Time complexity is O(2n) = O(n). Here's a sample C++ code I wrote, thought you'd be interested: """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" class Solution { public: bool checkValidString(string s) { int n = (int)s.length(); int bal = 0; // keep track of balance for (int i = 0; i < n; ++i) { if (s[i] == '(' || s[i] == '*') { bal++; } else { bal--; if (bal < 0) return false; } } bal = 0; // keep track of balance for (int i = n - 1; i >= 0; --i) { if (s[i] == ')' || s[i] == '*') { bal++; } else { bal--; if (bal < 0) return false; } } return true; } }; """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@techdose4u4 жыл бұрын
Nice...thanks
@256913314 жыл бұрын
Great Resource and great solution.
@techdose4u4 жыл бұрын
Thanks :)
@ishikajaiswal4735 Жыл бұрын
thankyou so much! crisp explanation.
@ashwanikumar42883 жыл бұрын
Nice work! Have you covered the O(1) space solution as well in any other video?
@ashvinkumhar58192 жыл бұрын
Thanks bro your explaination is very great.
@uplsove4 жыл бұрын
great explanation bro. this helped me a lot to understand this problem. Thank you
@techdose4u4 жыл бұрын
Welcome :)
@ramnathan42364 жыл бұрын
Thank you Sir.I tried using stack but only few testcases passed.
@techdose4u4 жыл бұрын
You must have done mistake somewhere. Recheck.
@krishnavamsichinnapareddy Жыл бұрын
Superb explanation ❤
@harifrahman80544 жыл бұрын
Will be helpful if u try to explain all the complex problems on leetcode
@techdose4u4 жыл бұрын
I picked leetcode just because of this April challenge. I will further include problems from here in future.
@arijitdey84193 жыл бұрын
Great explaination..Thanks a ton sir
@techdose4u3 жыл бұрын
Welcome
@ambujverma92874 жыл бұрын
very good explanation .... thank you
@techdose4u4 жыл бұрын
Welcome :)
@TheKarateKidd3 жыл бұрын
Great and clear explanation. Thank you!
@techdose4u3 жыл бұрын
Welcome
@harifrahman80544 жыл бұрын
Nice explaination
@techdose4u4 жыл бұрын
Thanks :)
@rhondawang26393 жыл бұрын
so smart, very clear thank u
@techdose4u3 жыл бұрын
Welcome
@karthikmadan8 ай бұрын
amazing explaination
@StockWithSayee Жыл бұрын
Loving your teaching :)))
@rajatbudania61814 жыл бұрын
Very Well Explained.!
@techdose4u4 жыл бұрын
Thanks
@manishnegi79264 жыл бұрын
thanks brother i was able to pass only 42 test cases. can you please make a video i.e related to how ur coding is too good ? and how u are able to solve the questions.
@techdose4u4 жыл бұрын
I will make it later....but for now I am focussing on study material content itself. 😅
@satyanarayanmohanty34154 жыл бұрын
Brilliant explanation. Thanks a lot.
@techdose4u4 жыл бұрын
Welcome :)
@somyasrivastava63153 жыл бұрын
What an amazing solution
@arjitgautam36510 ай бұрын
Well explained. Appreciated!!
@upharrastogi34644 жыл бұрын
Great Explanation Surya :)
@techdose4u4 жыл бұрын
Thanks
@fullstackacademy84004 жыл бұрын
Perfect approach
@techdose4u4 жыл бұрын
Thanks
@rishirajpaulchowdhury41633 жыл бұрын
Brilliant explanation. Thanks!!!
@chandankumarnayak74068 ай бұрын
class Solution { public boolean checkValidString(final String s) { int low = 0; int high = 0; for (final char c : s.toCharArray()) { switch (c) { case '(': ++low; ++high; break; case ')': low = Math.max(0, --low); --high; break; case '*': low = Math.max(0, --low); ++high; break; } if (high < 0) return false; } return low == 0; } }
@mohituniyal30084 жыл бұрын
superb explanation!
@techdose4u4 жыл бұрын
Thanks :)
@spk94344 жыл бұрын
Good job
@techdose4u4 жыл бұрын
Thanks
@ujjvalcodes36292 жыл бұрын
Wonderful solution!!
@borat_trades28604 жыл бұрын
i start to suspect that being good at these problems is a 2 step process: 1)SEEING many problems solutions, understanding them, memorizing them , 2) recalling solutions or TECHNIQUES when facing similar problems. i understand the solution for this problem now thx u! but how am i supposed to know it's 2 stacks?! i suspect i simply need to see many many solutions and then i will be able to know how to solve similar problems.. am i right?!
@techdose4u4 жыл бұрын
Actually you are partially correct. If you have experience then obviously many techniques will immediately come to your mind. But the most important thing about programming is to list down the requirement of the question that is the goal. After that you need to think what operations you would do as a straight forward process. Now you think how to achieve a given work in a shorter time. In this case, according to the unsaid requirement of the question, you needed to maintain postion of brackets and stars. This was an extremely important observation and everyone gets it sooner or later. So solving a question elegantly is all about Observations and experience :)
@mrinmoyhalder72932 жыл бұрын
thanks a lot..is this qsn has been asked in any interview ?
@amanpandey45502 жыл бұрын
Another approch - without using extra space' C++ Code- bool checkValidString(string s) { int maxDiff=0; int minDiff=0; for(auto ch : s){ maxDiff+=(ch=='(' || ch=='*') ? 1 : -1; minDiff+=(ch==')' || ch=='*') ? -1 : 1; if (maxDiff < 0) return false; minDiff = max(0, minDiff); } return minDiff==0; }
@ApnaVlogs-tj7do Жыл бұрын
nice explanation
@piercef73434 жыл бұрын
Fantastic explanation!
@techdose4u4 жыл бұрын
Thanks :)
@vishalmishra19374 жыл бұрын
supeerb explanation
@techdose4u4 жыл бұрын
Thanks :)
@himanshuchhikara49183 жыл бұрын
amazing explanation
@uditagrawal66033 жыл бұрын
I took the greedy approach: public boolean checkValidString(String s) { //Left pass int balance = 0; for(int i = 0; i < s.length(); i++){ if(s.charAt(i) == ')')balance--; else balance++; if(balance < 0)return false; } if(balance == 0)return true; balance = 0; //Right pass for(int i = s.length()-1;i >=0;i--){ if(s.charAt(i) == '(')balance--; else balance++; if(balance < 0)return false; } return true; } Ideology : Left pass : If total sum of ( and * is less then ) then string can never be balanced. Right pass : If total sum of ) and * is less then ( then string can never be balanced.
@rajeshbammidy1804 жыл бұрын
I tried using One Stack but my approach fails when we have start in bwn the "(" & ")"
@techdose4u4 жыл бұрын
Share your code to get help.
@ANANDJULU3 жыл бұрын
Thanks , liked and subscribed :)
@techdose4u3 жыл бұрын
Welcome :)
@sumengwang89184 жыл бұрын
Very helpful, Thank you very much!
@techdose4u4 жыл бұрын
Welcome :)
@rogiervdw4 жыл бұрын
Excellent explanation, thank you! Any chance you can explain the dynamic programming solution that's on LC?
@techdose4u4 жыл бұрын
If you have doubts then ask it. I don't have time now to make video for DP. Try solving and ask if you get stuck. You will get help.
@sauravchandra10 Жыл бұрын
Did anyone code the greedy approach which takes o(1) space?
@LTT_VKR4 жыл бұрын
I am still confused or I don't know how to find the complexity precisely...can you help with that or recommend any videos to watch on that...
@techdose4u4 жыл бұрын
Don't think time. Think how many computations you will you do. I have uploaded some basic time complexity videos. You can find blogs on geeksforgeeks as well.
@LTT_VKR4 жыл бұрын
@@techdose4u okay 👍
@humansofcrypto97464 жыл бұрын
Awesome soln!
@techdose4u4 жыл бұрын
Thanks :)
@f3-faithfitnessfinance4 жыл бұрын
Honestly I didn't get the question I was waiting for your video😂😂
@techdose4u4 жыл бұрын
🤣 what's this bro....please try to solve the question first yourself. It will be much more helpful.
@f3-faithfitnessfinance4 жыл бұрын
@@techdose4u 😂😂 I know I did for all the previous problems... But for this one...I tried understanding the question...I was like what's going on🙆♂️
@techdose4u4 жыл бұрын
😅 Then it's okay.
@hortsss4 жыл бұрын
same lol
@krishnkumar22774 жыл бұрын
could you refer some good resource of graph or please make a playlist of graph
@techdose4u4 жыл бұрын
I will soon start graph. You can refer geeksforgeeks for graph. It has everything you need. Just filter and study.
@adityasrinivas80444 жыл бұрын
Hi Sir , @10:28 , You said that by using Greedy Approach , we can solve this in O(1) extra space . How is this possible ?
@techdose4u4 жыл бұрын
The trick for O(1) space is simple. You can easily do it in 2 traversals for better understanding. In first traversal from left to right, balance all closing brackets using opening brackets + stars and 2nd traversal from right left, balance all opening brackets using stars and closing brackets already seen :)
@SR-we1vl4 жыл бұрын
Well it was a brilliant solution. But what if the interviewer asks do it in O(1) space!? What to do!?
@techdose4u4 жыл бұрын
maybe you can check leetcode discussions.
@weihua53054 жыл бұрын
Thank you very much
@techdose4u4 жыл бұрын
Welcome :)
@divakaram-xy6si8 ай бұрын
using pointers to keep track of open , star instead of stack would be a more optimal solution.
@rishi33084 жыл бұрын
Thanks a lot 😊
@techdose4u4 жыл бұрын
Welcome
@arthamsreenivas88584 жыл бұрын
Thanks Bro, i was trying with single stack with multiple trials did not work, how can we get 2nd stack idea without reading this solution before actual interview :) ?
@techdose4u4 жыл бұрын
You just need to practice. There is no easy way out.
@xapeuuu4 жыл бұрын
Do you know if ()() would be a accepted case? I don't see anything against it in the description.
@techdose4u4 жыл бұрын
Yes it will be accepted. Its answer will be TRUE. It is a valid string afteral.
@xapeuuu4 жыл бұрын
@@techdose4u Thanks. I was not getting over the fact that we are not necessarily matching the parenthesis in the most intuitive way. For instance in the case (*)) which is valid -> (()) we are matching the parenthesis with indexes [0,2] and [1,3]. If we knew that the final string would be (()) we would match them differently [0,3], [1,2]. After getting over that i was able to finally understand why this works :)
@techdose4u4 жыл бұрын
Great :)
@vishnugovindan85504 жыл бұрын
Could you shed some light on how this could be achieved in O(1) space if done greedy?
@techdose4u4 жыл бұрын
Someone had already posted in COMMENT section using 2 traversals, though not greedy but search it. You will get an easy O(1) space solution.
@surbhi1914 жыл бұрын
int count = 0; for(int i = 0; i=0; i--){ char c = s.charAt(i); if(c==')' || c=='*') count++; else{ if(count==0) return false; count--; } } return true;
@ruchi3158 ай бұрын
I can't solve any problem like this :(
@reroyallover9169 Жыл бұрын
thanos khus hua
@keerthi4424 жыл бұрын
Your really awesome bro
@techdose4u4 жыл бұрын
Thanks :)
@SembeiNorimaki4 жыл бұрын
8:10 I think this is incorrect: you have to start looking the open stack from the lowest element, otherwise you are compensating the lowest element in the open (0) with the highest in stack (4). You need to compensate the 0 with the lowest element in the stack that is higher than 0 (in this case 1), otherwise you might have elements in the open stack that no longer can be compensated. Imagine another example when you end with open= [0, 5] and stack=[2, 6]. If you compensate the 0 with the 6 you will not be able to compensate the 5 with the 2, but if you compensate the 0 with the 2 then you can compensate the 5 with the 6
@RaviYadav-xg2hy4 жыл бұрын
No, it's correct because the top of stack of parenthesis will always have the highest index compared with other stack elements below it....So the highest parenthesis index will take the highest star index to get balanced !!
@AkshayKumar-nh9nh4 жыл бұрын
Beautiful!
@techdose4u4 жыл бұрын
Thanks
@tanmayshishodia15074 жыл бұрын
Great explanation! Kudosss :)
@techdose4u4 жыл бұрын
Thanks :)
@anandpandey918 Жыл бұрын
//Using ArrayDeque class for implementating Stack (because Stack class is deprecated in java) (40% Faster) class Solution { public boolean checkValidString(String str) { ArrayDequeopenBracket=new ArrayDeque(); ArrayDequestar=new ArrayDeque(); int n=str.length(); for(int i=0;istar.size()) return false; while(openBracket.isEmpty()==false) { if(star.isEmpty()) return false; else { if(star.peek()>openBracket.peek()) { openBracket.pop(); star.pop(); } else return false; } } return true; } } //Using Array for implementating Stack (100% faster) class Solution { public boolean checkValidString(String str) { int n = str.length(); int[] openBracket = new int[n]; int[] star = new int[n]; int openTop = -1; int starTop = -1; for (int i = 0; i < n; i++) { char ch = str.charAt(i); if (ch == '(') openBracket[++openTop] = i; else if (ch == '*') star[++starTop] = i; else // ch==')' { if (openTop!=-1) openTop--; else { if (starTop!=-1) starTop--; else return false; } } } if((openTop+1)>(starTop+1)) return false; while (openTop !=-1) { if(starTop==-1) return false; if(star[starTop] < openBracket[openTop]) return false; openTop--; starTop--; } return true; } }