Reverse Words in a String (Amazon, Microsoft, MentorGraphics, MakeMyTrip) : Explanation➕Live Coding

preview_player
Показать описание
This is the 3rd Video on our Two Pointer Technique playlist.
In this video we will try to solve a very famous and interesting Problem "Reverse Words in a String".

We will do live coding after explanation and see if we are able to pass all the test cases.

Problem Name : Reverse Words in a String
Company Tags : Amazon (With variations), Microsoft, MentorGraphics, MakeMyTrip, Accolite, Adobe, Cisco, Goldman Sachs,Paytm, Samsung, SAP Lab

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

this channel is a gem...glad i found it

noone_xyz
Автор

bro's voice is so good that he has the potential to become a voice artist as well

arizimam
Автор

thanks bhaiya very useful and easy way of understanding the concepts
thanks a lot

lucusdukes
Автор

To be honest I know if I am able to solve questions on my coding test i somewhere know it is beacuse of u !!Thank you so much very greatful!!

queenshijain
Автор

your explanation is fabulous. your are so consistent, bhaiya can you tell me how to stay consistent.

vishalrai
Автор

bhaiya, TC will be O(N*M), where N is size of given string, M is average length of words in given string 🤔🤔
kyoki "i" pointer is also waiting for reversing character between "l" and "r", aur reverse krne me bhi toh time lagta hai, aur reverse me time depend karega ki words ki average length kitni hai

anshumaan
Автор

It is very helpful for everyone thanks bhaiya for provide this quality of content.

all_wood
Автор

s=s.substr(0, r-1) trims the trainling whitespaces but how is is preceeding whitespaces trimmed?? i cant understand

rajkumarroul
Автор

Can u please explain the TC of two pointer approach.

bhumikanaik
Автор

very very usefull for everyone thanks mik sir pehchana darshan desal linkdin pe meesege kiya tha apko

darshandesale
Автор

Bhaiya if( l < r) yeh condition bas normally nahi laga mere khyal se
agar 3-4 spaces lagatar ho ussey tackle karne ke liye bhi use hoga.

sunnyvlogs__
Автор

string reverseWords(string s) {
stringstream ss(s);
string token="";
string result="";

while(ss>>token){
if (!result.empty()) {
result = token + " " + result;
}
else {
result = token;
}
}

return result;
}

AMANKUMAR-ysiy
Автор

Can you please explain how the 2nd approach takes O(1) memory? Doesn't reverse in C++ take O(N) time and O(N) space?

SohelKhan-vtql
Автор

Hello sir ....I had a question. Will an interviewer all us to use inbuilt reverse stl during an interview?

prajwalshaw
Автор

Aapke leetcode mai dark theme nhi hai kya?

allmovies
Автор

Thanks for providing solution without using utility method.

Code in Java for approach 2

class Solution {
public String reverseWords(String s) {
int left = 0;
int right = 0;
StringBuilder sb = new StringBuilder(s);
reverse(0, s.length() - 1, sb);

int n = s.length();

int i = 0;
while (i < n) {

while (i < n && sb.charAt(i) != ' ') {
sb.setCharAt(right, sb.charAt(i));
right++;
i++;
}
if (left < right) {
reverse(left, right - 1, sb);
if (right < n)
sb.setCharAt(right, ' ');

right++;
left = right;
}
i++;
}

return sb.toString().substring(0, right - 1);
}

void reverse(int left, int right, StringBuilder sb) {
while (left < right) {
char ch = sb.charAt(left);
sb.setCharAt(left, sb.charAt(right));
sb.setCharAt(right, ch);
left++;
right--;

}

}

}

surajsidar
Автор

I didn't understand line no 21
Please explain

ankitagupta
Автор

two pointer se karlo, stack se pop karlo warna subproblems solve karlo recursive way se dhanyawad

Samtoosoon
Автор

class Solution {
public:
string reverseWords(string s) {
stringstream str(s);
string word, result = "";
int n = s.length();
while(str >> word)
{
result = word + " " + result;
}
return result.substr(0, n-1);
}
};
Approch 1 I'm getting wrong answer
please help

ankitagupta