Reverse Words in a String III - Leetcode 557 - Python

  Рет қаралды 17,465

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 20
@koalakakes
@koalakakes 11 ай бұрын
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)
@yahyaa4499
@yahyaa4499 Жыл бұрын
We can also concatenate an empty space at the end of the string first to tackle the edge case right?
@aryamankukal1056
@aryamankukal1056 3 ай бұрын
i did that yes
@danielsun716
@danielsun716 Жыл бұрын
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)
@simonhauguel9806
@simonhauguel9806 Жыл бұрын
For your "built-in" solution, consider generator-comprehension/map instead of the false "good" pattern "for i in range(len(X))" So, something like this : " ".join(a[::-1] for a in s.split())
@anonymoussloth6687
@anonymoussloth6687 Жыл бұрын
Couldn't u split the string using the ' ' (space), and reverse each element of that array, then join it?
@krateskim4169
@krateskim4169 Жыл бұрын
well yeah , but like he said at the start of the video, it wouldn't cover the essence of the problem.
@jamesarthurkimbell
@jamesarthurkimbell Жыл бұрын
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
@NeetCodeIO
@NeetCodeIO Жыл бұрын
It would depend on the problem. If you use them the solution to this problem is a one liner, so it's reasonable for a interviewer to disallow them I think
@SyWill0
@SyWill0 Жыл бұрын
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.
@asrahussain8642
@asrahussain8642 11 ай бұрын
he's talking about showcasing skills in interviews
@koalakakes
@koalakakes 11 ай бұрын
Yeah i highly disagree with the way he went about this. Over-complicating the problem doesnt give someone bonus points when im interviewing
@aditijain2448
@aditijain2448 Жыл бұрын
bro's voice changed
@pseudounknow5559
@pseudounknow5559 Жыл бұрын
Can we use a stack ?😅
@closingtheloop2593
@closingtheloop2593 6 ай бұрын
Is it bad to utilize python like this? class Solution: def reverseWords(self, s: str) -> str: s=s.split() l=[ ] for i in s: l.append(i[::-1]) return " ".join(l)
@anantom251
@anantom251 Жыл бұрын
class Solution { public: string reverseWords(string s) { int n = s.size(); int i = 0; int l = 0; int r = 0; while(i
@rohanchess8332
@rohanchess8332 Жыл бұрын
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
@UIEngineering101
@UIEngineering101 Жыл бұрын
Hey man what are these tools that you use to author these videos?
@christiancompiles5549
@christiancompiles5549 Жыл бұрын
Nice video!
@krateskim4169
@krateskim4169 Жыл бұрын
Man your voice is just so soothing.
Number of Good Pairs - Leetcode 1512 - Python
11:29
NeetCodeIO
Рет қаралды 15 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 309 М.
Cute
00:16
Oyuncak Avı
Рет қаралды 12 МЛН
Please Help This Poor Boy 🙏
00:40
Alan Chikin Chow
Рет қаралды 17 МЛН
АЗАРТНИК 4 |СЕЗОН 3 Серия
30:50
Inter Production
Рет қаралды 1 МЛН
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 80 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 232 М.
Leetcode 151 - Reverse Words in a String
8:04
Data Engineering Studies
Рет қаралды 287
Reverse Words in a String | LeetCode 151 | C++, Java, Python
13:58
Knowledge Center
Рет қаралды 184 М.
Extra Characters in a String - Leetcode 2707 - Python
21:11
NeetCodeIO
Рет қаралды 17 М.
OpenAI’s New ChatGPT: 7 Incredible Capabilities!
6:27
Two Minute Papers
Рет қаралды 198 М.
Find the Difference - Leetcode 389 - Python
11:24
NeetCodeIO
Рет қаралды 10 М.
Cute
00:16
Oyuncak Avı
Рет қаралды 12 МЛН