Valid Palindrome II

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

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

  • @vcfirefox
    @vcfirefox 3 года назад +11

    Dude. The number of leetocde solutions without explanations, but you took the time and effort to elaborate it out. You definitely deserve the best career possible. I am 10 years in the field and its rare to find people like you.

  • @GustavoDiaz93
    @GustavoDiaz93 4 года назад +21

    Whenever you say "pretty simple problem" I feel bad because to me it´s really hard (even though leetcode says "easy") I need more practice.

    • @sars-cov-2611
      @sars-cov-2611 4 года назад +2

      same. I solved it, but my algorithm has too much time complexity.

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

      Keep grindin

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

    Runtime is too strict, and edge cases are too annoying for this to be an "easy" problem.

  • @gokul5293
    @gokul5293 5 лет назад +3

    I like the way you approach the problems.

  • @asherintech
    @asherintech 4 года назад +3

    Awesome explanation, even without knowing Java you make these questions super easy to understand 🙏

  • @siddhantmisra5664
    @siddhantmisra5664 5 лет назад +4

    I was recently asked the spiral matrix question in the first round of my Facebook interview. Somehow managed to implement it.

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

    Awesome explanation man.....you are on next level 🔥🔥🔥🔥

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

    I was asked this question in Facebook interview.

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

    Thank you for such a nice explaination

  • @NishankSingla
    @NishankSingla 4 года назад +3

    I don't think it is O(n) complexity. Looks like O(n^2) beacuse of nested while loops for the worst case. If the input string is already a valid palindrome then it will be O(n).
    If we don't make a isPalindrome function and put 2 while loops directly inside the top while loop then internal 2 while loops will be O(2n) and with outer while loop it will become O(n^2).
    Please correct me if this logic is wrong.

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

      wrong, it is guaranteed O(N) in the worst case. When we find a mismatch, we loop through the rest of the string twice (substring from i + 1 to j, and substring from i to j - 1). If the first and last characters are mismatched, those 2 isPalindrome calls will be O(2N), and we immediately return after they are called. We NEVER call isPalindrome more than 2 times, so its guaranteed to be an O(N) solution in the worst case.

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

    Kevin excellent explanation ..

  • @TrollFreeInternet
    @TrollFreeInternet 5 лет назад +11

    Bro i know u probably have other important things to do but can u pls make more videos quickly?i love the way u explain and want to learn more from u..maybe make 2-3 videos a day? Xd

  • @jeezradz
    @jeezradz 4 года назад

    just perfect explanation. Thank you!

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад

      jeezradz thanks! If you like my explanations check out the interviewing service I launched thedailybyte.dev/

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

    I love this question

  • @drteeth11
    @drteeth11 5 лет назад +2

    Kevin, thanks for the great videos. I don't have Java experience, but I've been watching all your stuff because you explain in such an understandable way. You've surely been through hundreds of these problems, do you often run into a question that leaves you stumped for a bit? It would be interesting to see you code through a difficult problem for the first time.
    Question on prep vs. interview... when working through problems on LeetCode, I usually start small and keep iterating and executing/testing the code until i reach a satisfactory solution. Should the goal be to instead think through the problem and code the solution all in one go?

  • @freefortravel4317
    @freefortravel4317 5 лет назад

    I'm having trouble solving the problem 1172. Dinner Plate Stacks from Leetcode. I don't really understand the explanation in CTCI either. A video regarding this would be much appreciated. I've spent a fair bit of time with this problem. The methods I'm having trouble with are leftShift and specially join, if you check the CTCI implementation. Thanks!

  • @alanchavez16
    @alanchavez16 5 лет назад

    Thanks for helping us with these coding interviews. I have to take a part 2 - Amazon Full-Time SDE Assessment by this Friday. Would you give me any advice before I take it?

    • @psn999100
      @psn999100 5 лет назад +1

      Leetcode Amazon Card from Leetcode Explore section

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад +5

      Of course! My advice would be...
      1. stay calm! These questions can't be THAT hard if they're just a coding assessment so try to remember that when you're going through them!
      2. Read up on the most asked questions on LeetCode before taking it (hopefully you've been doing this :P)
      3. don't sweat it. What i mean by this is if it doesn't work out it's ok! There's tons of other incredible companies out there and you can always reapply. Remembering this tends to help me out.
      I hope these help and I'd love to hear how it goes tomorrow! Best of luck, you've got this!

    • @alanchavez16
      @alanchavez16 5 лет назад

      @@KevinNaughtonJr Thank you so much I appreciate it! I will do my best!

    • @alanchavez16
      @alanchavez16 5 лет назад

      @@psn999100 Thank you. I have been practicing in Leetcode that page is fantastic. I just barely graduated and I cant wait to learn more and more every time. Thanks for your help again I appreciate it.

    • @unknownman1
      @unknownman1 4 года назад

      @@alanchavez16 You got the job?

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

    very cool explanation! I was unnecessarily doing recursion in my helper function! This is better :)

  • @jonscheifley6863
    @jonscheifley6863 5 лет назад +1

    Great video. Keep up the good work!

  • @Yetsofaraway
    @Yetsofaraway 5 лет назад

    Kevin .. thank you for all the wonderful videos.
    I was wondering if just keeping a counter on the first while loop would suffice and if the counter goes over 1 then you can return a false instead of creating another method for checking the substring for a palindrome

    • @lizdenhup
      @lizdenhup 4 года назад +1

      I could be wrong but I don't think this would work; if there was a mismatch everything subsequent to the mismatch would also be a mismatch...because the 'non palindrome' character is essentially introducing a shift...

    • @amitadhikaree1801
      @amitadhikaree1801 4 года назад +1

      yes, i tried that approach. It didn't work. e.g "lcupuufxxfuupucul". Try checking your logic for this example.

  • @atift5465
    @atift5465 5 лет назад

    Nice vid but im a bit confused, how does the helper method work for the inner 2 indexes? Wouldnt the indexes overlap and not not beyond the while loop?

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

    Thanks Kevin!!

  • @MP-ny3ep
    @MP-ny3ep 2 года назад

    You might wanna try using a whiteboard to explain stuff.

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

    Seems to be timing out for me, isn't it O(n^2) and not O(n) ?

  • @antoniomartinez3429
    @antoniomartinez3429 4 года назад

    definitely a tricky one!

  • @doumkatekz
    @doumkatekz 5 лет назад

    Excellent thank you

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

    Thanks alot

  • @sandipbhaumik
    @sandipbhaumik 4 года назад

    It runs, Awesome!!!

  • @darod6098
    @darod6098 4 года назад

    Thank you.

  • @AJ_708
    @AJ_708 4 года назад

    great work!

  • @47tejash
    @47tejash 3 года назад

    When they give condition in the problem like Strings length is less than 50k chars etc. Do we need to explicitly add it in the code to check? Could you please explain.

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

    Thnx Kevin. This is helpful :)

  • @julietgeorge4858
    @julietgeorge4858 4 года назад

    yes to the music. lol

  • @vinayakumar2381
    @vinayakumar2381 5 лет назад

    Cool Explanation

  • @divitvivit8803
    @divitvivit8803 4 года назад +1

    its failed for input s="cbbcc"

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

    damn, i fell so stupid after watch this video ...

  • @monojit104
    @monojit104 5 лет назад

    how can I check palindrome by removing at most 2/3 characters ?

    • @darod6098
      @darod6098 4 года назад

      having a counter and decrementing it instead of directly returning the result of isPalindrome should work

  • @ehsanhosseini5861
    @ehsanhosseini5861 4 года назад

    Is this solution recursion? I can see you return one method inside another method, what you call this process?

    • @danavram8437
      @danavram8437 4 года назад +2

      No. Recursion is when you keep calling the same function over and over again (building a stack of consecutive function calls), with a different state, until you get to a base case.

    • @mohamedamer5904
      @mohamedamer5904 4 года назад

      Yes it can be, because actually you are solving this by doing the same thing twice but for smaller range and you can limit the number of the recursion calls by counter, here is code for it by Swift:
      func validPalindrome(_ s: String) -> Bool {
      var low = 0
      var high = s.count-1
      return validPalindrome(s, low, high, 0)
      }
      func validPalindrome(_ s: String, _ low: Int, _ high: Int, _ count: Int) -> Bool {
      if count > 1 {
      return false
      }
      let sChars = Array(s)
      var low = low
      var high = high
      while low < high {
      if sChars[low] == sChars[high] {
      low += 1
      high -= 1
      } else {
      return validPalindrome(s, low+1, high, count+1) || validPalindrome(s, low, high-1, count+1)
      }
      }
      return true
      }

  • @kunalchoudha
    @kunalchoudha 5 лет назад +1

    Hey Kevin...just FYI, this question's moved to hard category now.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад

      really? That's weird...I just checked again and it still says easy for me

    • @kunalchoudha
      @kunalchoudha 5 лет назад +1

      Kevin Naughton Jr. interestingly the question page shows it as easy... but if you search for it in search bar on home page, by typing 680...it says hard as difficulty level...😊

  • @bobij0
    @bobij0 5 лет назад

    For the second func chars are not compared for initial input vals i,j but are incremented right away...why is that still working I wonder.
    P.S awesome videos

    • @algoseekee
      @algoseekee 5 лет назад +2

      That's how post increment expressions work, they actually change the values *after* the values are being read.

    • @algoseekee
      @algoseekee 5 лет назад

      For better understanding try to solve this little puzzle: let i = 3; i++ - i - ++i = ? (you may execute this expression manually from left to right and check yourself in a browser's console after). Enjoy! :-)

  • @alankardingshsingh3701
    @alankardingshsingh3701 5 лет назад

    Hey Kevin, Please upload the video for longest palindrom substring Leetcode and word search II Leetcode. Can you please do it by tomorrow? Also, word search II using Trie.

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

    String problems are trash