LeetCode Decode String Solution Explained - Java

LeetCode Decode String Solution Explained - Java

Nick White

4 года назад

57,708 Просмотров

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


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

Shriram Balaji
Shriram Balaji - 03.06.2023 20:29

Thank you

Ответить
Hafid P
Hafid P - 12.04.2023 20:48

You are a genious. I would see that a companies would not hire you because you can discover many problems that they choose not to look at. I am hoping one day you make something brilliant out of your mind. The way you explain something is so effortless. I am jealous and amaze to see your talent.

Ответить
Swaroop Chakraborty
Swaroop Chakraborty - 16.10.2022 13:34

Hey, that's a good explanation, however instead of initializing the StringBuilder, you can just declare it before the iterative while loop and reset it every-time you encounter a new closing bracket and then start a fresh append from the pop. Something like this
StringBuilder segmentBuilder = new StringBuilder();
int segmentRepeatCount =0;
while(.....){
else if (encodedString.charAt(i) == ']') {
segmentBuilder.setLength(0);
segmentBuilder.append(words.pop());
segmentRepeatCount = counts.pop();
for (int k = 0; k < segmentRepeatCount; k++) {
segmentBuilder.append(result);
}
result = segmentBuilder.toString();
i++;
}....}
This will optimize the solution further.

Ответить
Anand Nettikadan
Anand Nettikadan - 10.09.2022 13:30

Thank you, well understood.

Ответить
Ritika Shishodia
Ritika Shishodia - 30.08.2022 19:15

Pls elaborate the explainnation taking some test case then write the code all are not pro in leetcode

Ответить
Ayush Pathak
Ayush Pathak - 23.08.2022 18:03

Currently it's 9ms in Leetcode..... : ()

Ответить
Daniyal Iqbal
Daniyal Iqbal - 11.07.2022 21:59

Hey what does the StringBuilder temp= new sB(result.pop()) do ?

Ответить
Mehmet Şen
Mehmet Şen - 22.02.2022 17:32

You have result stack and just push once and don’t do anything with it? This code will break in certain use cases.

Ответить
sachin sharma
sachin sharma - 19.02.2022 19:05

Sorry but u didn't explain it well

Ответить
Aman Sharma
Aman Sharma - 15.01.2022 14:42

you are awesome man

Ответить
Vaidehi Dharkar
Vaidehi Dharkar - 26.11.2021 08:45

Can someone compare iterative solution vs recursive solution, in terms of time complexity and suggest which one is better approach?

Ответить
Rama Tiwari
Rama Tiwari - 21.11.2021 14:00

Dont you get null pointer at line 21 when lets say string is [abc]2[a] ?

Ответить
Mukul Bahedia
Mukul Bahedia - 27.08.2021 14:04

U could have explained it with an example before starting implementation

Ответить
fawad raghib
fawad raghib - 24.08.2021 16:59

Great video. Just 1 suggestion, Could you dry run it and explain just any 1 example so beginers can understand it better.

Ответить
Em Cường
Em Cường - 20.07.2021 12:09

IR :(

Ответить
Kyle Weeks
Kyle Weeks - 11.07.2021 21:49

If there's a substring before a number, like "ab2[cd]", when does "ab" ever get added to res?

Ответить
Royce Le
Royce Le - 10.07.2021 00:40

Your solution and coding of it was nice, so thank you!
Suggestion:
-I think it would have been helpful if you ran through the solution with an example because the thought/intuition behind the solution could have been explained further for better understanding.

Ответить
Rishiraj Paul Chowdhury
Rishiraj Paul Chowdhury - 24.06.2021 19:45

I'd rather suggest a recursion solution that is much more intuitive and weirdly ran in 0ms.

string getString(string s,int n,int &i)
{
string res;

int ct=0;
string val;

while(i<n)
{
if(int(s[i])>=97 && s[i]<=122)
res.push_back(s[i]);
else if(s[i]>='0' && s[i]<='9')
{
val.push_back(s[i]);
}
else if(s[i]=='[')
{
int x=stoi(val);
val.clear();
i++;
string temp=getString(s,n,i);
for(int j=0;j<x;j++)
res.append(temp);
}
else if(s[i]==']')
return res;

i++;
}

return res;
}

string decodeString(string s)
{
int n=s.size(),i=0;
return getString(s,n,i);
}

Ответить
Anurag Verma
Anurag Verma - 29.04.2021 11:13

No one.
literally no one.
Nick White : "Uhh, Its pretty straight forward."

Ответить
AmazingSase
AmazingSase - 18.02.2021 23:53

not optimal solution, you gotta use recursion

Ответить
Adi Sekar
Adi Sekar - 14.02.2021 15:32

He seems high

Ответить
Z N
Z N - 27.12.2020 07:48

You forgot adding the # of this leetcode question

Ответить
Swapnika Pandey
Swapnika Pandey - 13.11.2020 08:55

whats the time complexity for this solution?

Ответить
Shabnam Mokhtarani
Shabnam Mokhtarani - 23.10.2020 07:00

Thank you for this video, Nick! Really appreciate what you do!

Ответить
Rakshith
Rakshith - 08.10.2020 05:33

instead of using res variable, it is better to use a StringBuilder as string concatenation is a costly operation

Ответить
zee
zee - 29.09.2020 20:08

This is the worst video i've seen you made. Please set a higher expectation for yourself.

Ответить
Prem Kumar Easwaran
Prem Kumar Easwaran - 17.09.2020 13:11

It is always suggested to NOT use Java's 'Stack' implementation during coding interviews and instead 'Deque' could be used. This is because java's Stack implementation extends Vector which is a synchronised data structure and hence must not be preferred for data which do not require synchronisation. Also, Deque is an interface implemented by LinkedList which holds it as an advantage over Stack, which is a class.

Ответить
Manan Surana
Manan Surana - 27.08.2020 02:05

What was the intuition behind using a stack in this question?

Ответить
Pareksha Manchanda
Pareksha Manchanda - 25.08.2020 15:08

nice and neat solution. Thanks for the upload!

Ответить
J
J - 09.08.2020 20:09

My coding practice routine usually involves being unable to solve medium level questions on leetcode and then coming to your channel for guidance. Your explanation, although a bit fast for my sorry brain, was very helpful.

Do u create these explanation videos once you've practiced the questions yourselves or is it just fresh off the list??

That brain freeze was so relatable. I go completely blank on many questions. Any tips??

Ответить
Arian Azin
Arian Azin - 09.07.2020 11:19

It would be nice if you explained the solution more seriously instead of emphasizing how blanked out you were. You're supposed to teach people who are seeing this for the first time, not those who are reviewing it again and already write the code by heart (like you did).

Ответить
Ousman D
Ousman D - 29.06.2020 19:14

Hell is a place where you're told to solve everything recursively and the call never stack exceeds.

Ответить
Chinmay Kulkarni
Chinmay Kulkarni - 26.06.2020 10:38

Hey Nick, thanks for the explanation. I came across this question today in an interview. I did not come up with 2 stacks approach. It's very hard to come up with this approach if you haven't already solved on leetcode.

Ответить
Genevieve Bolduc
Genevieve Bolduc - 16.06.2020 18:40

Runtime & Space complexity?

Ответить
Sneha Kavinkar
Sneha Kavinkar - 28.04.2020 01:58

why minus zero on line 12?

Ответить
Palak Gangwal
Palak Gangwal - 02.03.2020 01:44

Why would I think of using two stacks in the first place! :/

Ответить
Vivek Gr
Vivek Gr - 30.01.2020 14:25

excellent

Ответить
MORAMPUDI AKHIL
MORAMPUDI AKHIL - 29.12.2019 23:27

The logic is kinda very hard to think. How could you come up with this solution??

Ответить
Rakshith
Rakshith - 23.12.2019 00:27

it's always good if you could come up with tracing the algo and parallelly code.. so that it is easy to understand.. anyways good effort

Ответить
Tren Black
Tren Black - 23.09.2019 02:23

This question pissed me off to the depths of hell

Ответить
Saravanan Sarangan
Saravanan Sarangan - 09.09.2019 20:13

Nice logic Nick

Ответить
krishna
krishna - 09.09.2019 10:24

Super

Ответить