Reverse String - 3 Ways - Leetcode 344 - Python

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

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

  • @rideraff
    @rideraff 2 года назад +49

    Hey @NeetCode thanks A TON!! I couldn't have cracked FAANG interviews without your help. I interviewed with 3 FAANG companies and got offer letters from 2. This is the biggest milestone of my life and yes a dream come true. So thank you very much for all the great work you're doing :) You THE G.O.A.T. !!!!!

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

      Congrats, you deserve it!!! 🎉🎉🎉 And thanks for the kind words.

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

      @Aman Daredi Hi, I split my preparations into 3 parts. 1. Coding, 2. Behavioral, 3. System Design. Let me explain in fewer words as possible,
      1. Coding: I solved around 130 leetcode questions -> Blind 75 + Top questions from leetcode (I skipped some ofc :D ). Despite doing that I still got questions in the interview that were not available on leetcode or any other forums. I didn't completely bum out on those, I was able to implement 75% - 90% of the problem in the interviews. So, It's not about how many problems you solve, It's about understanding the common patterns and implementing them. Neetcode explains it so well in this video: ruclips.net/video/aa2ijyWBBIc/видео.html&ab_channel=NeetCode .
      Note: In the interviews, make sure you talk through your logic before coding. Mention the time and space complexities even before they ask. Also, explain how you measured time and space complexities for your logic.
      Quick Tip:
      Before jumping into leetcode, get better at basics. Data Structures, Common search and sort Algorithms, common Traversals, complexities, etc. Spend some time on this. Sometimes the interview problems may be so easy to implement but your lack of knowledge in the basics would fail you under the pressure (Trust me you do not want be in such situations)
      For example, instead of memorizing that Dictionary lookup is O(1), you should know what "lookup" is and how it works for a dictionary. Then you'll be able to calculate the complexity for any given data structure or algorithm easily.
      2. Behavioral: Amazon leadership principles cover most of the behavioral questions asked in any interview. I have a list of questions here - github.com/chickenwingstossed/behavioralquestions . This list has amazon questions and additional questions which I have gathered from my experience.
      3. System Design: This really comes down to the title, the company, and the job description of the position you have applied for. The recruiter may or may not give you details about the team but you can still ask them. Just make sure you learn the common System Design questions and Design Patterns.
      I hope the above helps. Best of luck to everyone.
      Never. Give. Up.
      Keep grinding \m/
      Thank you @NeetCode for giving me this opportunity to share my experience.

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

      congrats!

    • @maheshtiwari-yo8ux
      @maheshtiwari-yo8ux Год назад

      Hey @rideaff, I am also preparing for FAANG would like to connect and discuss . Please provide your linkedin profile.

  • @uditsharma5688
    @uditsharma5688 2 года назад +30

    Direct swap and stack solutions were intuitive for me but didn't notice that we could also use recursion. Thankyou for showing multiple solution instead of giving just the most efficient one. 👍

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

    This channel is extremely undervalued. It should have at least 500K subscribers.

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

    A big change from yesterdays Hard to todays... reverse a string

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

    Slicing works but Incase interviewer doesn't allow us to use built in functions then swapping is great

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

    No way, I love you because you upload videos everyday.

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

    Incase someone is looking for a solution for a string:
    def reverse(s):
    if len(s) == 0:
    return s
    return (reverse(s[1:]) + s[0])
    Here is how it works:
    Input: "hello"
    reverse("hello")
    reverse("ello") + "h"
    reverse("llo") + "e"
    reverse("lo") + "l"
    reverse("o") + "l"
    reverse("") + "o"
    => "" + "o"
    => "o" + "l"
    => "ol" + "l"
    => "oll" + "e"
    => "olle" + "h"
    => "olleh"

  • @Cloud-577
    @Cloud-577 2 года назад +6

    Please I need an advice. I have been doing leetcode questions for over 3 months now (this is my first time. I’m new to this). I have definitely improved but I don’t think I’m ready for technical interviews yet. How do I improve quickly? I really need to find a job asp for my own mental health wellbeing

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

      If you aren't already then you'll want to apply to any position you believe you are somewhat qualified for. The best practice you can get is actual technical interviews. You just need to be prepared to receive rejection emails or no response at all. Apply to the jobs you actually want once you feel a bit more comfortable with the technical interviews.

    • @Cloud-577
      @Cloud-577 2 года назад

      @@CoolerJ0k3r thank you for the advice. I appreciate it. You are right. I will never know until I try

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

      Heyy buddy,which position did you get?

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

    using slice trick print(s[::-1]) should work as well

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

      and you won't get hired, this problem is not a test of your syntax

  •  2 года назад +9

    I think the biggest reason the input is a "char[]" instead of "string" because in most languages strings are immutable. That means, you can't change them once you create it. Even if you think you change them, you actually create a new string object which automatically means that you need an extra memory.
    en.wikipedia.org/wiki/Immutable_object

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

    Isn't s.reverse() also works for this problem?

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

    Thank you for your amazing videos

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

    thanks for the videos!
    also, what software are you using to draw?

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

    Two other solutions,
    first another even less efficient stack solution:
    class Solution:
    def reverseString(self, s: List[str]) -> None:
    """
    Do not return anything, modify s in-place instead.
    """
    stack1,stack2 = [], []
    while s:
    stack1.append(s.pop())
    while stack1:
    stack2.append(stack1.pop())
    while stack2:
    s.append(stack2.pop())
    then another iterative solution :
    class Solution:
    def reverseString(self, s: List[str]) -> None:
    """
    Do not return anything, modify s in-place instead.
    """
    n = len(s)
    for i in range(n//2):
    s[i], s[n-i-1] = s[n-i-1], s[i]
    Making it 5 more or less efficient ways to do the same thing xD coding is fun

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

    Java
    class Solution {
    public void reverseString(char[] ar) {
    int i=0;
    int j=ar.length-1;
    while(i

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

    Would the space compexity of the recursive solution be O(1) for languages that support tail recursion?

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

    Why s=s[::-1] is not working? Could you please tell.

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

      F

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

      The problem requires you to modify s in place. What s=s[::-1] does is create a new reversed list and then assign it to s. If you do s[:] = s[::-1] it will actually copy each element of the reversed list into s.
      More in-depth explanation:
      Imagine you have a list and a variable arr which points to it. arr -> [1, 2, 3, 4]
      When you call reverseString(arr), inside the function the variable s is ANOTHER pointer to the input list. arr -> [1, 2, 3, 4] [1, 2, 3, 4] s -> [4, 3, 2, 1]
      But when you return from the function, s goes away since it's a local variable. LeetCode checks the value of arr and sees that you haven't reversed it.

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

    Could someone explain what would be the space and time complexity of reversing a string in JS using built in methods like str.split(‘’).reverse().join(‘’)

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

      Asymptotically O(n), but much slower.

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

    if possible to use nodeJs then please solve problem on nodeJs

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

    Aren't the first solution is n/2? And why the stack solution is faster then the first one.

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

      Big O notation doesn't care about the insignificant values. The only thing that matters is the part of the notation that is most significant. For example if you had an algorithm that took 3n^2 + 6n + 1 to run then the big O would be O(n^2) because n^2 is the most significant part. It would also not be n/2 because you access each element within the array once and there are n elements. Sorry if I didn't explain it very well.

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

      No big O notation disregards constants, and runtime is inconsistent, the second solution might not actually be faster on average

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

      The definition of big O notation is as follows:
      Consider 2 functions f(n) and g(n)
      f(n) is in O(g(n)) if there exists constants a and c such that for all n >= a, f(n)

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

    What software do you use to record your videos?

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

    I used a while loop, then i start print with the last index the ... Until the index 0

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

    Great going !!!!

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

    Love your stuff

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

    Thanks

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

    are you using your mouse to draw?????

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

      perhaps

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

      @@NeetCode what a boss

  • @anatoliy-gr
    @anatoliy-gr Год назад

    thanks for help!!!

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

    What about...
    return s[::-1]
    ???

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

      You need to reverse it in place, the s is still in the same order

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

    Other way:
    new_str_list = []
    new_str_list[:0] = "".join(str_list)[::-1]
    return new_str_list

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

    Please solve Leetcode 321 ..

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

    Oh great!

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

    Just came here to say [::-1]

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

    🥺❤️👊🏾

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

    second!

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

    Why is stack faster?
    According to me both of them are pretty much similar interns of no.of operations and loops