Reverse Words in a String III - Leetcode 557 - Python

Reverse Words in a String III - Leetcode 557 - Python

NeetCodeIO

8 месяцев назад

14,445 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

kbear
kbear - 17.10.2023 04:45

This was the first problem in my senior security engineer interview. (the other problem was practical terraform work in AWS which was way more applicable)

Ответить
William Long II
William Long II - 02.10.2023 23:48

Good video and explinations. Liked most of it. Honestly, the only thing I strongly disagree with is the idea that somehow, by using functions included in std library, you are somehow going against the spirit of the question somehow.
I have been a coder for a long while, and the whole reason std libraries exist in any language is to provide an easier yes and most the time more efficient way to accomplish a goal. There's no rhym or reason to reinvent a wheel, especially if you're trying and make a square one. Work smarter, not harder always. I've never seen in a production env most normal coders go oh i dont think reverse func is optmimal in std lib let me rewrite it lol. Good work, though. appreciate the content.

Ответить
Frontend Craft
Frontend Craft - 01.10.2023 18:07

Hey man what are these tools that you use to author these videos?

Ответить
ANANT OM
ANANT OM - 01.10.2023 17:20

class Solution {
public:
string reverseWords(string s) {
int n = s.size();
int i = 0;
int l = 0;
int r = 0;
while(i<n){
if(s[i+1]==' '||i+1==n){
r = i;
while(l<r){
swap(s[l],s[r]);
l++;
r--;
}
l = i+2;
}
i++;
}
return s;
}
};

DONE within 10 min.
took 2 attempts to take care of the empty space.

NO Extra space used.
S.C.-O(1)

Ответить
James Kimbell
James Kimbell - 01.10.2023 16:32

Are there really interviewers who would ban the use of split and join? I can imagine saying "okay, do it once with them, and now let's say what if you roll your own..." but to paint these super basic python methods as "cheating" just seems off

Ответить
Doctor X
Doctor X - 01.10.2023 15:23

We can also concatenate an empty space at the end of the string first to tackle the edge case right?

Ответить
krates kim
krates kim - 01.10.2023 15:02

Man your voice is just so soothing.

Ответить
Aditi Jain
Aditi Jain - 01.10.2023 14:36

bro's voice changed

Ответить
Anonymous sloth
Anonymous sloth - 01.10.2023 13:36

Couldn't u split the string using the ' ' (space), and reverse each element of that array, then join it?

Ответить
Pseudo Unknow
Pseudo Unknow - 01.10.2023 10:13

Can we use a stack ?😅

Ответить
Rohan Chess
Rohan Chess - 01.10.2023 07:27

Apparently I thought my solution had better memory complexity but it did not
class Solution:
def reverseWords(self, s: str) -> str:
prev = 0
res = ""

for i in range(len(s)):
if s[i] == " ":
for j in range(i-1, prev-1, -1):
res += s[j]
res += " "
prev = i+1

for j in range(len(s)-1, prev-1, -1):
res += s[j]

return res

Ответить
ChristianCompiles
ChristianCompiles - 01.10.2023 07:04

Nice video!

Ответить
Daniel Sun
Daniel Sun - 01.10.2023 05:49

From previous neetcode's solutions and some of my project experience, I would like to append an extra space string at the end. In this case, we will not consider that edge case
And I have to say this is really a good problem to practice how to deal with string, cause this kind of problem is very common to be asked as the 1st interview question.
One quick hint is that better be not modify any data in the original param given, maybe copy a new one is a good choice ,that is a detail.

class Solution:
def reverseWords(self, s: str) -> str:
# adding extra ' '
data = s + ' '
data = list(data)
l = 0
for r in range(len(data)):
if data[r] == ' ':
start, end = l, r - 1
while start < end:
data[start], data[end] = data[end], data[start]
start += 1
end -= 1
l = r + 1
data = data[:-1]
return ''.join(data)



# built-in function
data = s.split(' ')
for i in range(len(data)):
new_str_list = list(reversed(data[i]))
new_str = ''.join(new_str_list)
data[i] = new_str
return ' '.join(data)

Ответить