Valid Parentheses - Stack - Leetcode 20 - Python

Поделиться
HTML-код
  • Опубликовано: 1 дек 2024

Комментарии • 239

  • @NeetCode
    @NeetCode  2 года назад +51

    🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤

  • @mightyplayer6977
    @mightyplayer6977 2 года назад +165

    Thanks. Also you can add a check on top if the size of the string is odd. And if its is, return false before checking anything else. Because that's just O(1) check assuring that anything can be closed at all (you can't close each bracket if the sum of chars is not even).

    • @giriprasadg7964
      @giriprasadg7964 2 года назад +10

      That's smart

    • @adarshsasidharan254
      @adarshsasidharan254 Год назад +4

      cool

    • @AD-np2sh
      @AD-np2sh Год назад +9

      @@adarshsasidharan254 This is definitely a smart idea although remember some variations of this problem asked contain random chars other than brackets. This check would obv give a wrong result then.

    • @daze8410
      @daze8410 9 месяцев назад

      @@AD-np2sh Then you can keep a count each and if they are != return false

  • @cvxcfv
    @cvxcfv 3 года назад +265

    Better explanation than all the "Google employees" creating tutorials

  • @meBZcookie
    @meBZcookie 3 года назад +74

    this channel has been a blessing, please keep updating this channel :')

  • @erandro
    @erandro 2 года назад +40

    A base case can be added at the beginning that checks if the string length is odd and if so return false.
    This can optimize the time to O(1) if the string is odd (since there will be one bracket without a pair).

  • @nelsonfleig5024
    @nelsonfleig5024 10 месяцев назад +14

    Man I got asked this question today and totally bombed it. I had a recursive implementation that kind of worked but missed a bunch of cases. Well back to learning it is. Your channel and experience is a truly godsent.

  • @cxsey8587
    @cxsey8587 Год назад +8

    I was in the ball park with the hashmap, the stack, and the general idea, but handling the edge cases stumped me. Streamlined your code on my end just to make sure I understand it, thanks!

  • @danielazulay4936
    @danielazulay4936 2 года назад +13

    just changing the last return to: return stack == [] makes the runtime MUCH BETTER

    • @daviswest5286
      @daviswest5286 Месяц назад

      Definitely. "return not stack" also works and may be faster because it avoids an explicit comparison and directly evaluates the truthiness.

  • @peterkim1867
    @peterkim1867 2 года назад +33

    How the hell is this considered an easy problem?
    I definitely need more work -.-

    • @Nasirmah
      @Nasirmah 2 года назад +15

      Agreed, the trick is actually hard to pick so anyone who can solve most likely knows the trick or it would be hard. The code is easy though but not the strategy.

  • @melicanpelican8962
    @melicanpelican8962 6 месяцев назад

    luv that u explain the detail to all levels, like why a stack is helpful here & you lead listeners to suspect such a thing themselves ... super kind way, directly helpful 2 getting ppl into the mindset of solving these problems. mad thx

  • @singletmat5172
    @singletmat5172 3 года назад +18

    Very straightforward explanation, thank you!

  • @heyyou2646
    @heyyou2646 3 года назад +14

    Thank you for sharing with us all your knowledge, its a big thing to do

  • @hangovercub
    @hangovercub Год назад +15

    I only use c++ but I like looking at your explanations since you make it so simple to understand.

  • @toolworks
    @toolworks 2 года назад +17

    "return True if not stack else False" is a bit of a strange one IMO. You can just write "return not stack"

    • @pauljones9150
      @pauljones9150 2 года назад +6

      True, but it helps when he writes it out in dumb speech like that. In an interview it would probably also help to know that you can write it out both ways.

    • @illiat3164
      @illiat3164 2 года назад +2

      also, this one ## return len(p_stack) == 0 ## work fine

    • @katzy687
      @katzy687 2 года назад +2

      I actually prefer the guard clause, if stack -> return False, then a bare return True at the end

    • @arzelaascoli6765
      @arzelaascoli6765 2 месяца назад

      @@pauljones9150 I interviewed many candidates at Google; if someone wrote that on the whiteboard it'd be a red flag. No one should be writing production code this way and such unnecessary code would be flagged at review time.

  • @Melvinn27
    @Melvinn27 2 года назад +6

    Super helpful! Had trouble doing this on my own, learned a lot by doing your approach.

  • @Emorinken
    @Emorinken 3 месяца назад

    You’re actually good, I really hope I don’t fumble this opportunity and I get what I want with this

  • @expansivegymnast1020
    @expansivegymnast1020 2 года назад +2

    You did it way smarter than what I tried to. I made a hash table but I did one where the opening parentheses are they keys not the vales, the other way way is much simplier.

    • @kryddan
      @kryddan Год назад +4

      How is it any different?
      def isValid(s: str) -> bool:
      char_map = {"(": ")", "[": "]", "{": "}"}
      stack = deque()
      for c in s:
      if c in list(char_map.keys()):
      stack.append(c)
      else:
      if not stack or char_map[stack.pop()] != c:
      return False
      return not stack

  • @EjazAhmed-pf5tz
    @EjazAhmed-pf5tz Год назад +1

    i also used the stack data structure but use the if and else conditions therefor i came here to see more optimal and easy way and i got what i expect from your channel, thank you so much.

  • @ahyungrocks5509
    @ahyungrocks5509 6 месяцев назад +1

    This problem is extremely difficult for me. Thank you for your tutorial once again.

  • @christianondo9637
    @christianondo9637 8 месяцев назад +1

    The fact that this is easy is a little discouraging considering I fumbled around with it for several hours since I didn't know what a stack is. Glad to be able to learn this now though!

    • @prawnstars3160
      @prawnstars3160 8 месяцев назад

      were all in this together brother

    • @boredguy5805
      @boredguy5805 7 месяцев назад +1

      you aren't alone man lol. My advice is to not spend hours though, just go to the solution if it takes an hour

  • @julesrules1
    @julesrules1 2 года назад +1

    Love your clear explanation with simple code. You're the best. Thanks!

  • @oiramormedeiros
    @oiramormedeiros 2 года назад +5

    Hey, just want to let you know that I absulutely LOVE neetcode and all your videos. I'm a huge fan! Also, the code that you have on neetcode for this problem is a little different. I like the one from this video a lot better!

  • @PippyPappyPatterson
    @PippyPappyPatterson 2 года назад +5

    lots of edge cases on this easy to watch for

  • @wildworldexplorerstv
    @wildworldexplorerstv 5 месяцев назад

    Your explanation was perfect, yet I'm afraid I didn't understand that I wasn't able to solve this problem because I didn't know how to work with stack data structures. Good job my friend!

  • @waxinggibbons
    @waxinggibbons 6 месяцев назад

    Hey thanks for the video. I fought with this problem quite a lot. Had not heard about stacks before. I did not find this problem an easy one to be honest. Thanks for the explanation!

  • @yomamasofat413
    @yomamasofat413 6 месяцев назад

    After doing a few of these challenges, coding feels like tricks to manipulate stuff to get what you want. So different from other subjects

  • @DetectiveConan990v3
    @DetectiveConan990v3 7 месяцев назад

    this one took me a good 30 minutes. all I knew going into it was that it utilized the stack data structure. I was able to come up with the logic for it by thinking about how I would solve it just in my head if someone presented me with a sequence of parenthesis, keeping in mind the stack data structure. I used two lists instead to check if a bracket matches another

    • @DetectiveConan990v3
      @DetectiveConan990v3 7 месяцев назад

      @@mightyoranje did u know what a a stack was before hand? I think that’s the main thing

  • @RandomAnimeClip
    @RandomAnimeClip 3 месяца назад

    8:30 I think we dont need to necessarily check if the first char is closing parentheses then is not valid, we can 'return not stack' and get the case covered

  • @spiceybyte
    @spiceybyte 7 месяцев назад

    My first attempt was to use a deque as a stack but using a list works just fine. Thanks

  • @rudy871211
    @rudy871211 2 года назад +3

    I can't understand why stack can equal to closeToopen[i]. I mean that I know it prevent the empty array but I'm confuse.

    • @DeepakChauhan-wu7ei
      @DeepakChauhan-wu7ei 2 года назад +2

      It is just checking if stack is matching the keys in the hashmap. It can be equal only if it's already in the hashmap

    • @suphawatwong9438
      @suphawatwong9438 2 года назад

      @@DeepakChauhan-wu7ei if test with s="[{()}]" the first 3 loops of the function will make stack=["[", "{", "("] and stack[-1]="(", but from what I understand stack don't match any keys in the hashmap and the if stack and stack[-1] == closeToopen[c] statement can't be execute?

    • @DeepakChauhan-wu7ei
      @DeepakChauhan-wu7ei 2 года назад

      @@suphawatwong9438 It will keep on adding it top of the stack until it find closing parentheses like ')' in your case, now ')' =='(' it will remove it from stack and repeat the process until it's empty

  • @phoebesnest
    @phoebesnest 3 года назад +15

    Thanks!

    • @phoebesnest
      @phoebesnest 3 года назад +9

      Your channel helps me get an offer! Thank you so much!!! Keep on the great work :)

    • @NeetCode
      @NeetCode  3 года назад +7

      That's awesome! Congrats 🎉

  • @evgeshaky
    @evgeshaky 2 года назад +6

    does it make sense to check: if len(s) % 2 != 0: return False ?

    • @judowithkeishin
      @judowithkeishin 2 года назад +1

      not for this kind of solution, an odd length string would return false anyways

    • @rosewall8430
      @rosewall8430 Год назад +3

      I think it would make the code faster btw

  • @grzegorzszopinski7111
    @grzegorzszopinski7111 2 года назад

    i started from working around Dijkstra's Two Stack algorithm which uses similar logic and managed to do it without using hash map, but it required from me handling popping from an empty stack and also resulted in longer and dirtier (although simple to understand) code.

  • @aishwaryaranghar3385
    @aishwaryaranghar3385 3 года назад +3

    the channel gives me so much hope hehe

  • @user-fb4iv4me6g
    @user-fb4iv4me6g Год назад

    The rules are clear. An input string is valid if : 1) Open bracketes must be closed by the same type of brackets. 2) Open brackets must be closed in the correct order.
    A string }() should be valid. Because 1. and 2. are valid. The rules don't say anything about closed brackets needs to have an open bracket.

  • @gggeeh
    @gggeeh Год назад +2

    hey im not sure if i understand correctly, but wouldn't the stack.pop () function only remove the closed parenthesis, but leave the open parenthesis in the stack?

    • @MichaelShingo
      @MichaelShingo Год назад +5

      The closing parentheses are never added to the stack. So stack.pop() will only remove the opening parentheses that matches the closing one.

  • @aksedha3861
    @aksedha3861 Год назад +1

    class Solution:
    def ispar(self,x):
    opening_brackets = {'{': '}', '[': ']', '(': ')'}
    stack = []

    try:
    for b in x:
    if b in opening_brackets:
    stack.append(b)
    elif b in opening_brackets.values():
    if len(stack) == 0 or opening_brackets[stack.pop()] != b:
    return False

    if len(stack) > 0:
    return False

    except:
    return False

    return True

  • @mycasupatan3821
    @mycasupatan3821 2 года назад +1

    This is weird. When I copied the code of Neetcode and put it in Leetcode, it works. Then, when I debug it, it returns the wrong thing which is False. It is supposed to be True. I submit it and it works. The leetcode debugger might be glitchy or something.

  • @TharaMesseroux1
    @TharaMesseroux1 2 года назад +1

    Thank you, Neetcode! Have a happy holiday season!

  • @dannyjiujitsu
    @dannyjiujitsu Год назад

    Best explanation of this problem so far

  • @IbrahimLaeeq-v2g
    @IbrahimLaeeq-v2g 9 месяцев назад

    Cant we use an if else condition instead of using a hash-map although it doesnt really matter in this case because the space complexity for both these cases will still be O (1) because the values we store in the hash-map dont increase with the size of the input.

  • @dreamerslab
    @dreamerslab 4 месяца назад

    I guess I'm getting a hang of DSA 😅. I was able to solve this myself. Great content as always.

  • @wanderingcatto1
    @wanderingcatto1 Год назад

    While the solution here is easily understandable, a bigger challenge is how to get your train of thought to go in the right direction to begin with. I believe most people who have started off by writing the hash table in the form of {"(":")", "{":"}", "[":"]"}, which is intuitive and natural. From here on, the train of thought would have gone in a totally different direction.

    • @notphilly309
      @notphilly309 Год назад

      I agree. I think the intuitive thought is to write the hashmap like that but I can see other ppl eventually figuring out that its easier to check the values by setting it the other way

    • @deepb5204
      @deepb5204 Год назад

      I managed to solve this by myself, using same approach as shown in the video.
      -> My first thought wasn't even to create a hashmap, I only started with just a set of closed parenthesis.
      -> I then realized that whenever I see a closed parenthesis (matching from set), I will need to pop from stack.
      -> Then I realized, it would be easier to have a hashmap like shown in the video.
      so it wasn't actually that I thought of using a hashmap in the beginning, but rather at the end.

    • @VignetteQ
      @VignetteQ 11 месяцев назад

      @mrruixiangz You can still do it that way though, and the internal logic within the loop can be pretty much the exact same, just reversed. You just instead are checking if c is an open parentheses, then add it to the stack, and if not (c is a closed parentheses) then check if c equals the hashmap value for the last item in the stack (if stack not empty).

  • @zequue
    @zequue Год назад +1

    Hello great video but can someone explain why at the line if stack[-1] == closeToOpen we access the last element? Shouldn't the last element be a closing paranthese? Wouldn't that like trying to find an opening paranthese at the end of stack?

    • @slyspree
      @slyspree Год назад

      I was confused with the same doubt for alot of time but then finally understood.
      What you need to remember is that according to the logic of the code, only opening parenthesis' will be added to the stack, not the closing ones.
      Now with that in mind, consider the string to be " ( ) "
      The first character is ( which is an opening parenthesis which will be appended to the stack, now the next character in the string is ) which is closing parenthesis and according to the code we will now be checking the last element of the stack (we are not appending this character to the stack, just checking it). The last element of the stack matches with the value of the closing parenthesis in the hashmap.
      Apply the same logic to the other test cases and you will completely understand. Write it down on a paper don't try to do it all in your head.

  • @aspeno5613
    @aspeno5613 2 года назад

    Thank you! I was staring at the solution code thinking I would never understand 😂

  • @jonniboye6399
    @jonniboye6399 Год назад

    Noob question here: Even though it's not necessary for the solution, would it make sense at the beginning to count the number of each type of opening brackets in the string and compare to the number of each type of closing brackets in the string? Therefore if for example a string had 3 '(' in it but only 2 ')' then we would automatically know the output would be false. Would this be useful for saving time at all?

    • @andrechen846
      @andrechen846 11 месяцев назад

      no bc then u still wont know if theyre matching up to each other or not, it could be like : (] [(, theres 2 of each paranthases and bracket but still its incorrect

  • @konradhunter1407
    @konradhunter1407 7 месяцев назад

    I kept doing s.replace(‘()’, ‘’) til empty, but your way looks much faster.

  • @lch99310
    @lch99310 Год назад +2

    Sorry. I am pretty new to programming. What does term stack mean in here?

    • @josephjoestar4318
      @josephjoestar4318 Год назад

      This is 3 months late sorry.
      A stack is Last in First out (LIFO).
      Think of putting(pushing/adding) the opening brackets into a thin glass jar and with it being so thin, you can only take out the last item you placed into it.
      I'm sure you've already worked this out but just in the off chance.

  • @chikeegonu7437
    @chikeegonu7437 2 года назад

    Wow!. A very cut clear explanation. Thanks

  • @Coral_dude
    @Coral_dude Год назад

    wondering why we are starting with the inverse order for the hash map meaning ")":"(" instead of "(":")" seems logical to keep the order in the way it will be presented as True

  • @ehsanshafiei4510
    @ehsanshafiei4510 10 месяцев назад +2

    why do you just pop last index

  • @davyroger3773
    @davyroger3773 2 года назад

    Dijkstras two stack algorithm helped me come to this conclusion

  • @AMIT-hw8fo
    @AMIT-hw8fo 7 месяцев назад

    You can use Switch Concept

  • @ahsanulhaque4811
    @ahsanulhaque4811 Год назад +1

    I would just return "not stack". Should be enough.

  • @asd-sl1kv
    @asd-sl1kv Год назад +7

    return not stack

  • @SAMtryIT
    @SAMtryIT Год назад +2

    Great video! The subtitles are in Vietnamese, can they be changed to English? There are some parts that I don't understand, although I still have to translate into traditional Chinese. If possible, I would be very grateful.

  • @alifeoflaurels
    @alifeoflaurels 3 года назад +1

    in the closeToOpen[c] line, does 'c' mean complement? is that the syntax of writing the complement of a hashmap ?

    • @FratPatriot99
      @FratPatriot99 3 года назад

      yeah you're getting the value for key "c" in the closeToOpen dictionary, it's just the python syntax

    • @arjavtongia1331
      @arjavtongia1331 2 года назад

      thats just the iterator in the 7th line "if c in closeToOpen:"

  • @chrisholland6366
    @chrisholland6366 10 месяцев назад

    Good. ternary at the bottom is unnecessary. not stack is already a bool expression

  • @ErezEl
    @ErezEl 6 месяцев назад

    But he didn’t add anything to the stack? It’s still empty, so how does this work?

  • @edwardlocnguyen
    @edwardlocnguyen 2 года назад +1

    Why is the closeToOpen dictionary not considered another object, making the space complexity O(2n) ?

    • @arjunreddy2647
      @arjunreddy2647 2 года назад +1

      because it’s length is not a factor of the string length it is a constant length of 3

    • @JayPatel-ce4jp
      @JayPatel-ce4jp 2 года назад +1

      no matter how big the input size of the array is, the hashmap stays at a constant O(3) which is just O(1) and O(n) + O(1) is just O(n)

  • @bananesalee7086
    @bananesalee7086 2 года назад

    oh right we can actually just use a list in reverse order.
    saves me from messing with deque data structure

  • @ayanrosey8597
    @ayanrosey8597 2 года назад +1

    How come map here does not factor into space- time complexity?

  • @muditdahra4780
    @muditdahra4780 Год назад

    leet code is showing answer is wrong but if I change last line of code to "return True if stack else false " than it work properly ..
    btw nice explanation

  • @bincebee
    @bincebee 11 месяцев назад

    Great Explanation 👌👌

  • @tanoybhowmick8715
    @tanoybhowmick8715 2 года назад

    Thank you, please keep up the good work.

  • @tywong8237
    @tywong8237 2 года назад +1

    test failed for input = " ] "
    I added a condition to the if statement on line 8
    if c in closeToOpen and len(stack) != 0:

    • @thetechies2259
      @thetechies2259 Год назад

      i think u didnt put return statement in 1st else condition

  • @aakashyadav2100
    @aakashyadav2100 Год назад

    nice and clear explantion

  • @charles8xu
    @charles8xu Год назад

    When I was watching this video, I found that the automatically generated subtitles are in Vietnamese, can it be changed to English? Thank you for uploading these videos

  • @diftq8191
    @diftq8191 2 месяца назад

    doesnt this return false if the test case is ( [ ) ]? Because it returns false since ( doesnt equal [ in the third iteration of s

    • @sarahf1871
      @sarahf1871 Месяц назад

      it does, which is correct; ( [ ) ] is not valid way of using parentheses. ("Open brackets must be closed in the correct order")

    • @diftq8191
      @diftq8191 Месяц назад

      @@sarahf1871 Yeah I noticed that shortly after as well, thanks for letting me know

  • @GT-cv3xu
    @GT-cv3xu Год назад

    thanks for good explanation!

  • @pcccmn
    @pcccmn 2 года назад +1

    I feel kinda bad tho...This question is categorized as "easy" but I still had to go to NeetCode...

    • @levibeam100
      @levibeam100 2 года назад +1

      don’t worry about it. Just keep going. Took me over 80 problems To consistently get easy problems done

  • @dennistsai5348
    @dennistsai5348 Год назад

    It’s super clear!

  • @leilachihab8820
    @leilachihab8820 2 года назад

    Thanks so much for the helpful video. Why do you need to write 'if not stack' in the last line?

    • @MichaelShingo
      @MichaelShingo Год назад

      If you've gone through the whole string, and there is still an opening parenthesis left in the stack, it means that there was no matching closing parenthesis for that one. That makes the string invalid.

  • @gianniprocida3332
    @gianniprocida3332 2 года назад

    Excellent!! Thank you so much

  • @rosewall8430
    @rosewall8430 Год назад

    Im pretty worried about myself. I feel silly bcz i just tried for 9 hours straight to solve this and it never worked, i just dont feel enough for solving this kind of problems, like, how tf i would come up with this solution?

    • @Tomson419
      @Tomson419 Год назад

      same bro, same. how do you feel about it now (4 months later)???

  • @SenthilKumar-re1wc
    @SenthilKumar-re1wc Месяц назад

    can it be done recursivly?

  • @Color-Theory
    @Color-Theory 4 дня назад

    I kind of hate that the question assumes that strings will only consist of braces. My solution was wrong because I made it robust enough to allow for other characters between the braces T_T

  • @AndrewLor
    @AndrewLor 3 года назад +5

    was asked question this today!

    • @anirudhkannan9
      @anirudhkannan9 3 года назад +2

      how did it go?

    • @usa5450
      @usa5450 Год назад

      Which company??

    • @AndrewLor
      @AndrewLor Год назад

      @@usa5450 SAP

    • @usa5450
      @usa5450 Год назад

      ​@@AndrewLor ok 👍🏻

    • @usa5450
      @usa5450 Год назад

      ​@@AndrewLor you got selected??

  • @biomed3d503
    @biomed3d503 2 года назад

    New to python. Can you explaine more about return Ture if not stact else false?

    • @maurorodrigues1380
      @maurorodrigues1380 2 года назад +1

      Think of it as using if/else logic. If not stack (if the stack is empty) it means every pair had its match, so we return True. If there's still something in the stack, it means there are still brackets left to close, so we return False

  • @NavyaVedachala
    @NavyaVedachala Год назад

    is anything wrong with doing it like this?
    class Solution:
    def isValid(self, s: str) -> bool:
    hashMap = {
    ")" : "(",
    "}" : "{",
    "]" : "["
    }
    stack = [];
    for c in s:
    if c in hashMap:
    if not stack or stack.pop() != hashMap[c]:
    return False
    else:
    stack.append(c)

    return True if not stack else False

  • @ardhidattatreyavarma5337
    @ardhidattatreyavarma5337 Год назад

    awesome explanation

  • @nishansinghdhillon1714
    @nishansinghdhillon1714 2 года назад +2

    How will stack[-1] always match with the correct ?
    In case it's something like [{()}]

    • @Nasirmah
      @Nasirmah 2 года назад +2

      The best way to visualize is to loop thought it which reads from the left, as long as you see open one then you add to the stack, the trick is once you see close one then the one before it must be open one of its type which was last added to the stack. The first three loops will be open. Once you reach fourth ) then you check the stack of last one added which was ( so it matches.

    • @MrRenaissance17
      @MrRenaissance17 2 года назад +2

      Testing this with the debug feature on VSCode does help a lot with wrapping your head around the functionality of this.

    • @cimbot
      @cimbot 2 года назад +1

      @@MrRenaissance17 Do you have any tutorial on how to do this?

    • @minciNashu
      @minciNashu 2 года назад +1

      the first closed brace you encounter must match the last open brace i.e stack[-1]

  • @kartikeyporwal3539
    @kartikeyporwal3539 2 года назад +3

    Congratulations to me who screwed the interview round over this problem.
    What's worst is I'd already watched this video 1-2 months back!
    Shame.

    • @milktea2755
      @milktea2755 2 года назад

      it's okay, you'll get it next time !!

    • @milktea2755
      @milktea2755 2 года назад

      may I ask how you screwed up, did you forget how to do some part of it?

    • @nikhil_a01
      @nikhil_a01 Год назад +1

      Which is why watching videos is not that helpful. You need to build your problem solving skills so that you can do it even if you've forgotten the video.
      I just repeated this problem 5 months after I first did it. It took me 8 minutes instead of 20, and the code was more readable.
      This isn't to put anyone down. Just to say that people shouldn't become dependent on solution videos, and they should ensure they're actually improving.

  • @MrRenaissance17
    @MrRenaissance17 2 года назад

    Can we interchange not stack for True?

  • @Riteshyadav-vp7db
    @Riteshyadav-vp7db Год назад

    I am still not able to process how is this a easy difficulty problem in leet code.

  • @alexkhaerov
    @alexkhaerov 3 года назад +5

    Please use underscore, respect pep.

  • @nickieferrante5487
    @nickieferrante5487 3 месяца назад

    Can we solve it WITHOUT using a stack?

  • @DanielPratt-om9ti
    @DanielPratt-om9ti Год назад +1

    you're explanations are like someone else showed you how to do it and you memorized it, you go so deep on the most obvious things but completely ignore detail in the more complex aspects like skipping over the hash map, you literally said 'you can probably guess what im doing here' no dude this is literally the most complex aspect of the entire program.

    • @DanielPratt-om9ti
      @DanielPratt-om9ti Год назад

      perfect example 8:30-8:50 help us out brother

    • @SuubUWU
      @SuubUWU Год назад

      I think it's "obvious" to an audience that's actively grinding out leetcode problems.
      If you haven't finished your first year of CS where the proof was drilled into your head or ran into the concept of a hashmap or hashing in general, then yes it seems complex.
      As for everyone else, searching by index is pretty common data structure and that it has O(1) for adding, deleting, and searching. It comes from analyzing the best case, avg case, and worst case (big omega, big theta, big O) of other algorithms and finding a balance of sacrificing space complexity for time complexity.
      There's a handful of other proofs that are taken for granted due to the audience usually coming from a CS / University background, like why the average height of a BST is log(n) or

  • @drakenevadie19_ThanhNguyenDo
    @drakenevadie19_ThanhNguyenDo Год назад

    Super helpful!!!!

  • @edwardteach2
    @edwardteach2 3 года назад +1

    U a God

  • @Levelord92
    @Levelord92 2 года назад +2

    I can't even solve an easy problem... I feel really stupid

  • @miaseriki
    @miaseriki Год назад

    still not sure why we're using a stack dsa, help!

  • @Yougottacryforthis
    @Yougottacryforthis Год назад

    the order was super not clear in the wording of ther question

  • @RandomAlexus
    @RandomAlexus Год назад

    Awesome content.

    • @NeetCode
      @NeetCode  Год назад +1

      Thank you so much 🙏

  • @gwulfwud
    @gwulfwud 10 месяцев назад

    you're awesome!

  • @aleksey3750
    @aleksey3750 2 года назад +1

    You are really cool!

  • @jugsma6676
    @jugsma6676 Год назад

    I was doing this:
    ```
    def isValid(self, s: str) -> bool:
    stack = []
    for c in s:
    if c in ["(", "{", "["]:
    stack.append(c)
    else:
    if not stack:
    return False
    if (stack[-1] == "(" and c != ")") or (stack[-1] == "{" and c != "}") or (stack[-1]== "[" and c != "]"):
    return False
    else:
    stack.pop(-1)
    return True if len(stack) == 0 else False
    ```

  • @krateskim4169
    @krateskim4169 Год назад

    Thank you so much

  • @tdog7925
    @tdog7925 2 года назад

    every video has invalid/ wrong coding solution. why?

    • @NeetCode
      @NeetCode  2 года назад +1

      Do you mean the code does not get submitted? I tried it and it does, I'm guessing you may have a typo

    • @aartigoswami2584
      @aartigoswami2584 9 месяцев назад

      @@NeetCodeyes dude,
      there is small mistake in code, it’s not working. Any ways thanks to share this.

  • @jennyxu1078
    @jennyxu1078 2 года назад +2

    I'm sorry but I don't think this works with input only has opening parentheses, like '[{'

    • @NeetCode
      @NeetCode  2 года назад +2

      Try it on that test case and see what happens

    • @nikhil_a01
      @nikhil_a01 Год назад +2

      Yeah, seriously. What's with _guessing_ that the code is wrong? Just run it and find out.