Parsing A Boolean Expression - Leetcode 1106 - Python

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

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

  • @janaSdj
    @janaSdj 2 дня назад +34

    Problem is very easy and straight forward using Stack; you are complicating it using recursion.

    • @nihalbhandary162
      @nihalbhandary162 2 дня назад

      Also an optimal solution with O(n) time and space complexity.

    • @Starfalll6783
      @Starfalll6783 2 дня назад +1

      Could you share your code

    • @business_central
      @business_central День назад

      Can you share the Stack solution?

    • @rajsuriyang3427
      @rajsuriyang3427 День назад +2

      We are using a stack.He is using a call stack that's it😂.

    • @mohammedsuhail.s192
      @mohammedsuhail.s192 День назад +1

      of course bro this problem is basic stack problem i dont why neetcode do this kind of solution he want to chenge himself for beginners also

  • @JustSomeYTChannel557
    @JustSomeYTChannel557 2 дня назад +5

    Amazing solution! One small way to improve it ever so slightly is, instead of using an array to store all the sub-problems and then doing `any` or `all` on it, we can simply save a Boolean variable. We can initialize it as "result = NULL" and do something like:
    "if (result == null) { helper(s) } else if (c=='&') { helper(s) && result!! } else { helper(s) || result!! }"
    This way we can improve on a large array of booleans, in case of many, many subproblems for a specific & or |

  • @Matem-sc1ic
    @Matem-sc1ic День назад +1

    Very elegant solution

  • @santanu29
    @santanu29 2 дня назад +1

    I always get intrigued by these kinds of solution. I heard somewhere that coming up with these solutions in an interview is not easy, if you have never seen in before. I was think in the stack route. I have a Google interview next month (fingers-crossed), will be referring your Neetcode 150 for the preparation for sure.

    • @ineshi
      @ineshi 2 дня назад +1

      good luck!

  • @satyadheeraj6081
    @satyadheeraj6081 День назад

    Bro gave a legendary solution! In every run it beats 100% of the solutions

  • @faeancestor
    @faeancestor 2 дня назад +8

    this is pure genius.. how long will it take for a noob like me to come as powerful as you in the IDE?

  • @BekaBirhanu-os6vm
    @BekaBirhanu-os6vm 2 дня назад +8

    this is my first time seeing neetcode write unclear code.

  • @MP-ny3ep
    @MP-ny3ep 2 дня назад +1

    Wouldn't have understood this problem without you

  • @siddharth-gandhi
    @siddharth-gandhi 2 дня назад +7

    any reason to do it this way compared to much simpler 2 stack solution?

  • @zweitekonto9654
    @zweitekonto9654 День назад +1

    This gets more interesting if you now add precedence.

  • @amitgoyal8760
    @amitgoyal8760 2 дня назад

    I just thought about prefix notation and how we use a stack there as soon as I saw this problem.
    And to be honest, the stack approach seems more intuitive, easy, and equally optimal as well.

  • @IK-xk7ex
    @IK-xk7ex 2 дня назад

    It's the fist time I think my own solution is more elegant that the solution of the teacher :)

  • @RanjanG-cq6wg
    @RanjanG-cq6wg День назад

    amazing code explanation. great work

  • @IK-xk7ex
    @IK-xk7ex 2 дня назад

    I solved it by using 2 stacks: first for operations, second for "t","f"

  • @Yougottacryforthis
    @Yougottacryforthis День назад

    The iterative solution 'going inside-outside" is way more elegant

  • @prashant8041
    @prashant8041 2 дня назад +1

    namastey!

  • @lucidity1230
    @lucidity1230 День назад

    I found this to be the easiest to understand in my opinion..... Definitely an annoying problem, would not have any clue how to solve this in a real interview
    class Solution:
    def parseBoolExpr(self, expression: str) -> bool:
    stack = []
    for c in expression:
    if c == ')': # If we've reached the end of a sub expression
    sub_expression = []
    while stack[-1] != '(':
    sub_expression.append(stack.pop())
    stack.pop() # Remove the '('
    operator = stack.pop()
    if operator == '!': # Evaluate the sub_expressions
    result = not sub_expression[0]
    elif operator == '&':
    result = all(sub_expression)
    elif operator == '|':
    result = any(sub_expression)
    stack.append(result)
    elif c != ',': # Anything that's not a comma, add it to the stack
    if c == 't':
    stack.append(True)
    elif c == 'f':
    stack.append(False)
    else:
    stack.append(c)
    return stack[0]

  • @finemechanic
    @finemechanic 2 дня назад

    Now I see how deceiving the recursive approach is. The problem looks naturally recursive, but the implementation is complicated. I used stack mostly automatically and it turned out way simpler.
    P.S. Something has broken at Leetcode. I have never got solution time less than ~30ms in the past year, but recently I am getting 10ms and even 0ms evaluation. They may have upgraded Python to a new version.

  • @business_central
    @business_central День назад

    I am still confused about helper() being used with no parameters, can anyone provide more details?

    • @SarweshParsewar
      @SarweshParsewar День назад

      index is being shared for all the recursive calls so no need for any parameters you can kind of think it as a while loop where we are jumping the index

  • @alh-xj6gt
    @alh-xj6gt 2 дня назад

    Could skip further evaluation with first encounter for _false_ in &() and _true_ in |()?
    Example: &(t,&(t,&(f,&(t,&(t,&(t,&(t,&(t,&(t)))))))))
    with reaching the f there is no need to evaluate rest of the &() case.

  • @sanjeevmurmu6505
    @sanjeevmurmu6505 2 дня назад

    why didn't you use stack for this

  • @floriankubiak7313
    @floriankubiak7313 День назад

    My non-refactored code got better performance than yours - do I get a referral? ;)
    Btw: huge shame on you for not using switch/match case loops :(

  • @parthokr-o5l
    @parthokr-o5l 2 дня назад

    that's not a hard question if you know top-down parser

  • @eamytb
    @eamytb 2 дня назад

    Just use the freaking stack, man

  • @nofollowrobotstxt
    @nofollowrobotstxt 2 дня назад +2

    im switching to healthcare to be a doctor without borders because i like crossing boundaries. please stop uploading.

  • @AlexNikiporenko
    @AlexNikiporenko День назад +2

    O(n) stack solution:
    class Solution:
    def parseBoolExpr(self, expression: str) -> bool:
    reduce = {
    '&': lambda x, y: x and y,
    '|': lambda x, y: x or y
    }
    stack = []
    opened = False
    for c in expression:
    if c in {'&', '|', '!', '('}:
    stack.append(c)
    elif c == 't':
    stack.append(True)
    elif c == 'f':
    stack.append(False)
    elif c == ')':
    arr = []
    while stack[-1] != '(':
    arr.append(stack.pop())
    stack.pop()
    if stack[-1] in reduce:
    op = stack.pop()
    stack.append(functools.reduce(reduce[op], arr))
    elif stack[-1] == '!':
    stack.pop()
    stack.append(not arr[0])
    return stack[0]

    • @floriankubiak7313
      @floriankubiak7313 День назад

      How is the performance?

    • @AlexNikiporenko
      @AlexNikiporenko День назад

      @@floriankubiak7313 super quick, "beats 97.36% of python3 submissions"

  • @top10z38
    @top10z38 2 дня назад

    python is love

  • @Sharky-1507
    @Sharky-1507 2 дня назад

    First!

  • @freecourseplatformenglish2829
    @freecourseplatformenglish2829 День назад

    Below is my 2 stack solution. In case anyone need it.
    class Solution:
    def parseBoolExpr(self, expression: str) -> bool:
    opStack = []
    resStack = []
    for ch in expression:
    if ch in ('!', '&', '|'):
    opStack.append(ch)
    else:
    if ch == ')':
    boolVals = []
    while resStack[-1] != '(':
    boolVals.append(resStack.pop())
    # remove extra '(' from the top
    resStack.pop()
    op = opStack.pop()
    if op == '!':
    val = boolVals.pop()
    if val == 'f':
    resStack.append('t')
    else:
    resStack.append('f')
    elif op == '|':
    res = 'f'
    for val in boolVals:
    if val == 't':
    res = 't'
    break
    resStack.append(res)
    elif op == '&':
    res = 't'
    for val in boolVals:
    if val == 'f':
    res = 'f'
    break
    resStack.append(res)
    else:
    resStack.append(ch)
    # print(opStack, resStack)
    return True if resStack[0] == 't' else False