Microsoft Loves This Coding Question! | Reverse Words in a String - Leetcode 151

preview_player
Показать описание
FAANG Coding Interviews / Data Structures and Algorithms / Leetcode
Рекомендации по теме
Комментарии
Автор

Sorry, it should be "the sky is blue" -> "blue is sky the"!

GregHogg
Автор

Bro is taking 3 left turns instead of 1 right

AngryEgg
Автор

That code is waaaay too complicated. You only need to do 4 steps:
- split on spaces
- reverse the array
- filter the array for any empty strings (the null between the two spaces, at the beginning and at the end)
- join the array with a space again

so: " hi there ".split(" ");

no need to trim separately, or to track things manually in a loop

danielhaveman
Автор

Bro the type of guy to park the furthest from the grocery store

AverageSensei
Автор

I think a more elegant (and memory efficient) solution is to iterate over the chars backward and while char != ' ', add the chars to a string rev_word. Each time the loop breaks, ans += ' ' + rev_word[::-1] and rev_word = '' (also, add on the termination of the for loop if rev_word isn't empty). Same time complexity, but lower memory complexity. Plus no extra logic necessary to handle weird extra whitespaces.

Memory complexity for your solution is O(k) where k is the number of words, this is O(1).

sophiophile
Автор

Dude. Strip + reverse loop + character buffer using a stack + store resulting words in the buffer in an array every time you find a whitespace. Then join the array at the end. 2n runtime

merveillevaneck
Автор

Thanks. This is an accepted answer in Python:
' '.join(s.split()[::-1])

salism
Автор

You can do it with no additional memory by just reversing the whole string first and then reversing each word(using two pointers technic)

antonivanov
Автор

normal people: Just use split() or the equivalent in your language... solved in one minute.
me with arm assembly without any libraries : "this little maneuver is gonna cost us 51 years!!"






ya ik using asm is stoopid for coding interviews.

thescientist
Автор

Regex replace all `\s.*?` matches with ` ` and then trim() would work for the spacing issues, no?

jjpaq
Автор

Honey even the conversation to the wording is mathematically aligned together

TonyFarley-gicv
Автор

If you use .split() or .split(None) instead of .split(' ') then you don't need for-loop nor .strip(' ')

def rev(txt): return ' '.join(txt.split()[::-1])

And if you really need to use space (or other char) in .split(' ') then you may use filter(None, list) to remove empty strings from list

def rev(txt): return ' '.join(filter(None, txt.split(' ')[::-1]))

Автор

I think the for loop is overkill. Just: str.split(' ').filter(s => !!s).reverse().join(' ')

corbinfonville
Автор

You don't need a custom for loop to do a super common operation...
"...".split() does exactly what you would expect, removing all whitespace, so e.g.
" this is a \t string ".split()
Gives you
["this", "is", "a", "string"]

So the way to do this task in Python is just
" ".join(reversed(inp.split()))

Eknoma
Автор

Can you use split and join? Then maybe a trim instead of all that parsing

briandavis
Автор

Without using so much memory for large inputs, we could use a the regular stack tecnique that gets flushed on spaces. You do it in O(n) time too.

Phrozends
Автор

the sky is blue
blue is the sky

bro failed test cases in dry run

FakeDumbDummy
Автор

Even though if we don't provide the separator to split, still it splits the string as expected without including those spaces in the list.
I did it in one line:
return "

prasadsawant
Автор

can you use the split function ? btw I love your content!

pound
Автор

sir what about, or . or any other punctuation. ususally there is no space between words n punctuation

chandings