Reverse words in a string

preview_player
Показать описание
This video explains how to reverse words in a string. This is one of the most frequently asked interview and coding round question for software companies. My microphone got damaged and hence the sound quality is way below par but i am trying to get it fixed as soon as i can. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
Рекомендации по теме
Комментарии
Автор

@Tech dose How to solve this problem using space O(1) and recursion?

anushraghav
Автор

If suppose I want to swap only certain words in the string, how can I do it?
example: John knows Python and Sundar knows Java
I want to swap Java with Python and Python with Java.
There are solutions on net, but NO one explains like you do.
Thanks :-)

MKSundaram
Автор

I CAME HERE FOR CODE. I KNOW THE APPROACH. BUT DOING SOME MISTAKE IN CODING PART, BUT THIS NOT COVERED

sushant
Автор

I really love all the videos of your channel. Could you please provide the code link of your explanation? It will be really helpful

AnjaliKumari-zmzo
Автор

Hi Budy, i have started to like your videos lot. And really appreciate ur help. Just one suggestion try to cover the solutions in constant space as well. It will help everyone :) thanks

varunsadhu
Автор

I got approach instantly but did not know why was getting error in implementing.
Took a lot of time in getting the mistake! Leaving my working C++ code here so others can refer:

class Solution {
public:
string reverseWords(string s) {
string res;
int n = s.length();
//s = " how are you "
//Firstly remove all the excessive spaces
if(s[0] != ' ')
res.push_back(s[0]);
for(int i=1; i<n; i++)
{
if(s[i] != ' ')
res.push_back(s[i]);

else if(s[i] == ' ' && s[i-1] != ' ') //insert space only after word ends so when s[i] is ' ' but s[i-1] is not, this means the word has ended
res.push_back(' ');
}
if(res.back() == ' ') //we do not want space after the last word
res.pop_back();

int r = res.length();
int countSpaces = 0;
for(int i=0; i<r; i++)
{
if(res[i] == ' ')
countSpaces++;
}
if(countSpaces == 0) //if only one word is present in string, return as it is
return res;
// res = "how are you"
//Now we need to reverse the whole string and then reverse each word
for(int i=0; i<r/2; i++)
swap(res[i], res[r-1-i]); //res = "uoy era woh"

int i, j=0;
for(i=0; i<r; i++) //reversing each word
{
if(res[i] == ' ') //when ' ' is reached, start of any word is given by j and end is given by i-1. j is updated everytime a previous word is reversed. for the last word, j stands at start of last word and i becomes equal to res.size(). Handle last word separately
{
for(int start=j, end=i-1; start < end; start++, end--)
swap(res[start], res[end]);
j = i+1;
}
}
//after for loop ends, j will be at starting of last word and i will be at 1 vaue ahead of end of last word
for(int start=j, end=i-1; start < end; start++, end--)
swap(res[start], res[end]);
return res;
}
};

chetanraghavv
Автор

Hi sir can you please tell me how to reverse a word like
I like java
a vaje kili

dewanganskitchen
Автор

We can split string on the basis of space and traverse it from end, this will also do the task

rajatarora
Автор

Dude, you are great! The way you explain is by far to the point. Thks a lot!

DhruvilPatel
Автор

Even i guess the logic when I saw the problem statement. Is this the only way to do this? There is no other way?

sakthim
Автор

int main ()
{
int i, j, c=0;
char s [50], temp [50];
gets (s);
for (i=0;s [i]!='\0';i++)
c=c+1;
for (i=c-1, j=0;i>=0;i--, j++)
{
temp [j]=s[i];
}
printf ("%s\n", temp);
return 0;
}
Sir i did upto this sir. I reverse entire string and then how to reverse each and every word . As you said in the class.

babuk
Автор

Superb 👍
Can you explain your thought process while approaching such a tricky problem?

prasad.patil
Автор

@Tech dose where can i find the code for this in c++

srividhyaparthasarathy
Автор

Awesome explaination sir....pls add more videos on time and space complexity

amitpaunikar
Автор

input :she is good
iam getting output as good si ehs the next two words are not reversing help me

bharatnagasunil
Автор

code :)

#include<bits/stdc++.h>
using namespace std;

int main()
{
// reversing the words
string st;
cout<<"enter the string";
getline(cin, st);
int i =0 ;
cout<<"\n";
int n = st.length();
// reversing the entire string
for(int i = 0; i <n/2;i++)
{
swap(st[i], st[n-i-1]);
}
int start =0;
char temp;
int j =0;

// revversign the string with gap as end
for(i=0;i<n;i++)
{
if(st[i]==' ' || st[i]=='\0')
{
for(int begin=j, end=i-1 ; begin<(i+j)/2 ; begin++, end--)
{
swap(st[begin], st[end]);
}
j=i+1;
}
}
// printing the string
for(int i = 0 ; i <n;i++)
{
if(st[i] == ' ')
{
cout<<"\n";
}
else
{
cout<<st[i];
}

}
return 0;
}

sainathreddy
Автор

@techdose from where can i learn the time complexity, i mean i need tutorials to calculate the time compley of the codes

shariquekalam
Автор

can u provide the code of this logic??

jasmeenkaur
Автор

Code -
class Solution {
public:
string reverseWords(string s) {
s.push_back(' ');
string ans ="";
stack<string> st;
string temp="";
for(int i=0;i<s.size();i++){
if(s[i]!=' '){
temp.push_back(s[i]);
}
else if(temp.size()!=0){
st.push(temp);
temp="";
}
}
while(!st.empty()){
ans+=st.top();
ans+=' ';
st.pop();
}
ans.pop_back();
return ans;
}
};

atharvaverma
Автор

These 106 people who dislike this video .those who are in 4th year and their placements are about to come .

LetszGoo