Reverse String - 3 Ways - Leetcode 344 - Python

preview_player
Показать описание


0:00 - Read the problem
1:02 - Iterative Solution
4:16 - Stack Solution
6:18 - Recursive Solution

leetcode 344

#microsoft #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

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.

rideraff
Автор

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. 👍

uditsharma
Автор

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

gianniprocida
Автор

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

NeetCode
Автор

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

shakhaoathossain
Автор

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

Cloud-
Автор

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.

Автор

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

__________________________
Автор

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

harveylesterarcilla
Автор

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

NotKevinNguyen
Автор

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

fieryfeather
Автор

thanks for the videos!

also, what software are you using to draw?

christinaphan
Автор

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"

floydian
Автор

What software do you use to record your videos?

algorithmo
Автор

Could someone explain what would be the space and time complexity of reversing a string in JS using built in methods like

lymphtics
Автор

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

kritikabhateja
Автор

if possible to use nodeJs then please solve problem on nodeJs

domeshsinha
Автор

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

dandendrasingh
Автор

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

numberonep
Автор

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

_sam_ael