Reverse String - 3 Ways - Leetcode 344 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024

ความคิดเห็น • 62

  • @rideraff
    @rideraff 2 ปีที่แล้ว +46

    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: th-cam.com/video/aa2ijyWBBIc/w-d-xo.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 ปีที่แล้ว

      congrats!

    • @maheshtiwari-yo8ux
      @maheshtiwari-yo8ux 11 หลายเดือนก่อน

      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 ปีที่แล้ว +21

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

  • @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 ปีที่แล้ว +5

    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 5 หลายเดือนก่อน

      Heyy buddy,which position did you get?

  • @shakhaoathossain5032
    @shakhaoathossain5032 ปีที่แล้ว +1

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

  •  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

  • @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

  • @__________________________6910
    @__________________________6910 2 ปีที่แล้ว +1

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

  • @NotKevinNguyen
    @NotKevinNguyen 2 ปีที่แล้ว +1

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

  • @christinaphan5124
    @christinaphan5124 2 ปีที่แล้ว +2

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

    • @NeetCode
      @NeetCode  2 ปีที่แล้ว +2

      I use Paint3D

  • @algorithmo134
    @algorithmo134 2 ปีที่แล้ว

    What software do you use to record your videos?

  • @ahmadtarawneah
    @ahmadtarawneah 2 ปีที่แล้ว +2

    Thank you for your amazing videos

  • @kritikabhateja110
    @kritikabhateja110 2 ปีที่แล้ว +2

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

    • @issamuhsen1245
      @issamuhsen1245 ปีที่แล้ว +1

      F

    • @nikhil_a01
      @nikhil_a01 ปีที่แล้ว +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.

  • @harveylesterarcilla9329
    @harveylesterarcilla9329 2 ปีที่แล้ว +1

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

    • @juansymontano
      @juansymontano 6 หลายเดือนก่อน

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

  • @fieryfeather
    @fieryfeather 2 ปีที่แล้ว

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

  • @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)

  • @vdyb745
    @vdyb745 2 ปีที่แล้ว

    Great going !!!!

  • @pauljones9150
    @pauljones9150 2 ปีที่แล้ว

    Love your stuff

  • @weishin
    @weishin 2 ปีที่แล้ว +1

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

    • @NeetCode
      @NeetCode  2 ปีที่แล้ว +1

      perhaps

    • @weishin
      @weishin 2 ปีที่แล้ว

      @@NeetCode what a boss

  • @domeshsinha7670
    @domeshsinha7670 2 ปีที่แล้ว

    if possible to use nodeJs then please solve problem on nodeJs

  • @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.

  • @anatoliy-gr
    @anatoliy-gr ปีที่แล้ว

    thanks for help!!!

  • @saraex8558
    @saraex8558 2 ปีที่แล้ว

    Thanks

  • @_sam_ael
    @_sam_ael 9 หลายเดือนก่อน

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

  • @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

  • @tanoybhowmick8715
    @tanoybhowmick8715 ปีที่แล้ว

    Oh great!

  • @rajatagrawal7016
    @rajatagrawal7016 2 ปีที่แล้ว +1

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

  • @JimmyBrock
    @JimmyBrock ปีที่แล้ว

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

    • @juansymontano
      @juansymontano 6 หลายเดือนก่อน

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

  • @vineethnc8934
    @vineethnc8934 2 ปีที่แล้ว

    Please solve Leetcode 321 ..

  • @pira9850
    @pira9850 2 ปีที่แล้ว

    second!

  • @ericlefort
    @ericlefort 2 ปีที่แล้ว +1

    Just came here to say [::-1]

    • @kritikabhateja110
      @kritikabhateja110 2 ปีที่แล้ว

      I was also thinking this.
      But in leetcode it didnt work. I dont know why

    • @issamuhsen1245
      @issamuhsen1245 ปีที่แล้ว

      F

  • @kalahari8295
    @kalahari8295 2 ปีที่แล้ว

    🥺❤️👊🏾

  • @gonzalochacaltana2735
    @gonzalochacaltana2735 ปีที่แล้ว

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