Backspace String Compare - Leetcode 844 - Python

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 27. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    đŸ„· Discord: / discord
    🐩 Twitter: / neetcode1
    🐼 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: ‱ Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: ‱ House Robber - Leetco...
    Problem Link: leetcode.com/problems/backspa...
    0:00 - Read the problem
    0:30 - Stack Explanation
    2:23 - Two Pointer Explanation
    6:29 - Coding Explanation
    leetcode 844
    #neetcode #leetcode #python

Komentáƙe • 31

  • @user-j5ja95
    @user-j5ja95 Pƙed 9 měsĂ­ci +10

    I've been refreshing your page everyday for new videos haha thank you for all that you do, if I ever see you in person I'd love to treat you for coffee or yummy food for all the times you helped me prepare for interviews lol

  • @lifeofsanjai
    @lifeofsanjai Pƙed 9 měsĂ­ci +1

    i read through every LC solutions explaining the optimised solution but nothing was helpful. but now you made it clear. thanks a lot man for the explanation.

  • @S3aw0lf
    @S3aw0lf Pƙed 9 měsĂ­ci +1

    spent over an hour trying to solve for O(1) space complexity even came up with a solution using Ascii value of the strings but the edge cases are making it diffcult so now came to watch ur vid :).Keep up the consistency

  • @servantofthelord8147
    @servantofthelord8147 Pƙed měsĂ­cem

    That follow-up was a nightmare. Thanks for going over it.

  • @thetensordude
    @thetensordude Pƙed 9 měsĂ­ci +1

    Thanks to your roadmap

  • @AnnuSharma-mi9lb
    @AnnuSharma-mi9lb Pƙed 9 měsĂ­ci

    Okay, wait is over, thanks Navdeep
    ❀

  • @shreymehra6774
    @shreymehra6774 Pƙed 9 měsĂ­ci

    Could you also explain the problem 1094. Parallel Course 2? not able to understand and mostly people use lru cache instead of implementing dp

  • @rugvedb9842
    @rugvedb9842 Pƙed 9 měsĂ­ci +1

    I can never really see where to use Stacks. I would have solved this problem in a different way but the stack solution you shared is definitely better!

    • @HtotheG
      @HtotheG Pƙed 9 měsĂ­ci +7

      My advice is that it just comes with practice seeing when to use a stack, queue, heap or other like structure. What helped me the most was using NeetCode's roadmap to see problems that utilize the same datastructures together in a group to try to identify patterns. One pattern for me is that stacks are often used when we want to deal with the element at [i-1] based on some condition at [i]. So in this problem, we might want to delete the previous character based on seeing a '#" in the current character. Hope this helps at all, best of luck!

  • @AngelRojas-iy6yw
    @AngelRojas-iy6yw Pƙed 9 měsĂ­ci

    Hi. Is there an option on the neetcode website to reset the roadmap progress? Sometimes I like to start from scratch and I manually un-select all the questions ive done.

    • @NeetCodeIO
      @NeetCodeIO  Pƙed 9 měsĂ­ci

      There should be a trash btn on the /practice page

    • @AngelRojas-iy6yw
      @AngelRojas-iy6yw Pƙed 9 měsĂ­ci

      @@NeetCodeIO I see it. Thank you. Keep up the good work, super helpful!

  • @GameFlife
    @GameFlife Pƙed 9 měsĂ­ci

    Hehe clever neet

  • @oniii-chan_
    @oniii-chan_ Pƙed 9 měsĂ­ci

    I also got stuck at the last test case and hacked my way through while True:

  • @brainstormbalaclava5384
    @brainstormbalaclava5384 Pƙed 5 měsĂ­ci

    should't we name functon "nextValidIndex" instead of "nextValidChar" ?)

  • @dohunkim2922
    @dohunkim2922 Pƙed 3 dny

    can anyone comment on this code? works but kinda slow
    class Solution:
    def backspaceCompare(self, s: str, t: str) -> bool:
    def replace_char_at_index(s, index, new_char):
    return s[:index] + new_char + s[index + 1:]
    def delete(word):
    i = 0
    flag = 0
    while i < len(word):
    if word[i] != "#":
    if i > flag:
    word = replace_char_at_index(word, flag, word[i])
    flag += 1
    else:
    print(flag > 0)
    if flag > 0:
    flag -= 1
    print(word)
    i += 1
    print(flag)
    print()
    return word[:flag]
    a, b = delete(s), delete(t)
    return (a == b)

  • @sumitsharma6738
    @sumitsharma6738 Pƙed 9 měsĂ­ci +2

    Let's write some neetcode today ❀

  • @swastik07
    @swastik07 Pƙed 9 měsĂ­ci +1

    def func(s):
    stack = ''
    for i in s:
    if stack and i == '#':
    stack = stack[:len(stack)-1]
    elif i != '#':
    stack += i
    return stack

    return func(s) == func(t)
    I have solved it like this

    • @jacobsmith538
      @jacobsmith538 Pƙed 9 měsĂ­ci +1

      This is so much better, I wanted to implement this but I didn't know how to do the subtraction of stack = stack[:len(stack)-1] Thank you!

    • @leeroymlg4692
      @leeroymlg4692 Pƙed 9 měsĂ­ci +1

      it would be a little more efficient to use an array as your stack instead of a string because slicing operations on a string isn't as efficient as popping from an array

    • @swastik07
      @swastik07 Pƙed 9 měsĂ­ci

      @@leeroymlg4692 stack becomes so efficient but I only did it to reduce space

    • @swastik07
      @swastik07 Pƙed 9 měsĂ­ci

      @@jacobsmith538 Thank you

    • @Rlh9xc
      @Rlh9xc Pƙed 9 měsĂ­ci

      That’s O(n) space complexity though right?

  • @swastik07
    @swastik07 Pƙed 9 měsĂ­ci

    Time and space complexity ---- > O(n) and O(1)
    def func(s):

    ln = 0

    stack = ''

    for i in s:
    if stack and i == '#':

    ln -=1
    stack = stack[:ln]
    elif i != '#':
    stack += i

    ln += 1

    return stack


    return func(s) == func(t)

    • @sabu4539
      @sabu4539 Pƙed měsĂ­cem +1

      cuz of the string, it still counts as o(n)

  • @abhinavnair4577
    @abhinavnair4577 Pƙed měsĂ­cem

    Here's a simpler version of the code -
    class Solution:
    def backspaceCompare(self, s: str, t: str) -> bool:
    def remove_char(s):
    stack = []
    for char in s:
    if char=='#' and stack:
    stack.pop()
    elif char!='#':
    stack.append(char)
    return stack
    return remove_char(s)==remove_char(t)

    • @yasinunlu443
      @yasinunlu443 Pƙed měsĂ­cem

      The space complexity is O(n) here. The optimized code is O(1) space complexity, which requires two pointer algorithm.

    • @abhinavnair4577
      @abhinavnair4577 Pƙed měsĂ­cem

      @@yasinunlu443 in two pointer approach also the worst case complexity will be O(n)