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
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--; } } }
I solved in different way.. Like run loop from last till first get the words n append it to result so overall words will be reverse //REVIEW CODE & PLACE SUGGESTIONS private static String reverseWordsInStringTwoPointer(String str) { String result = ""; int i = str.length() - 1; int first = str.length() - 1; int last = str.length() - 1; char[] stringArray = str.toCharArray(); while (i >= 0) { if (stringArray[i] != ' ') { if (stringArray[last] == ' ') { last = i; } first = i; } if (stringArray[i] == ' ' && stringArray[last] != ' ') { result = result.concat(str.substring(first, last + 1).concat(" ")); last = i; first = i; } if (i == 0 && stringArray[i] != ' ') { result = result.concat(str.substring(first, last + 1).concat(" ")); } i--; } return result.substring(0, result.length() - 1); }
Let's understand it with a simple example : String str = "we will evlos shtam" Suppose in the above string I ask you to only reverse "evlos" part and make it "solve" For that you need the position of "e" and "s" of "evlos" so, let's say index of "e" is = l and index of "s" is = r Because, the function reverse(beginning_position, end_position) requires the beginning and end of something that you want to reverse. so position for "e" will be = str.begin() + l; and position of "s" will be = str.begin() + r; So, we pass these two positions to reverse() function Then it will only reverse "evlos" and make it "solve" So our str will now look like = "we will solve shtam" I hope that helps you and I was able to explain it well. Let me know if there is any further doubt. Thanks a lot for watching 😇
@@anshumaan1024 time complexity of swapping two variables is O(1) because we create a temp variable and shuffle the values among the two variables and temp. Now reversing will swap characters from each corresponding ends ie forward and reverse until we reach middle so O(1) xO(N/2) which is O(N/2) which is basically O(N) and not O(N²)
@@anshumaan1024 no it will be O(N) only assume you are reversing a 11 letter sentence which has 2 ,5 letter words time complexity of reversing the whole would be O(11) and reversing each words would be O(5+5+1) ;1 is for traversing space.Bascally O(11) for reversing the whole sentence and O(11) for reversing each words which is O(N)+O(N) or O(2N ) or which is simplified O(N)
@@anshumaan1024 you are reversing each words after u have reversed the whole sentence and not that you are reversing each words after each swap/reverse of sentence so O(N)+O(N) =O(N) and not O(N*M)
bro's voice is so good that he has the potential to become a voice artist as well
this channel is a gem...glad i found it
thanks bhaiya very useful and easy way of understanding the concepts
thanks a lot
Great explanation
very nice explanation bhaiya
Thank you 😇❤️
It is very helpful for everyone thanks bhaiya for provide this quality of content.
Thank you so much 😇
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
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!!
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;
}
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--;
}
}
}
Bhaiya yeh line ki kya need thi bataoge ?
if (right < n)
sb.setCharAt(right, ' ');
your explanation is fabulous. your are so consistent, bhaiya can you tell me how to stay consistent.
s=s.substr(0,r-1) trims the trainling whitespaces but how is is preceeding whitespaces trimmed?? i cant understand
Thanks for the clean code bro
wao
Great Explanation
Can u please explain the TC of two pointer approach.
very helpful
I solved in different way.. Like run loop from last till first get the words n append it to result so overall words will be reverse //REVIEW CODE & PLACE SUGGESTIONS
private static String reverseWordsInStringTwoPointer(String str) {
String result = "";
int i = str.length() - 1;
int first = str.length() - 1;
int last = str.length() - 1;
char[] stringArray = str.toCharArray();
while (i >= 0) {
if (stringArray[i] != ' ') {
if (stringArray[last] == ' ') {
last = i;
}
first = i;
}
if (stringArray[i] == ' ' && stringArray[last] != ' ') {
result = result.concat(str.substring(first, last + 1).concat(" "));
last = i;
first = i;
}
if (i == 0 && stringArray[i] != ' ') {
result = result.concat(str.substring(first, last + 1).concat(" "));
}
i--;
}
return result.substring(0, result.length() - 1);
}
very very usefull for everyone thanks mik sir pehchana darshan desal linkdin pe meesege kiya tha apko
very good
ek number
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?
Reverse will not take O(n) space. It can be done Inplace.
For example - “abcd”
Swap a with d -- “dbca”
Now swap b with c - “dcba”
sach me ?@@codestorywithMIK
two pointer se karlo,stack se pop karlo warna subproblems solve karlo recursive way se dhanyawad
👌
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.
Yes. Would be good to try with an example
@@codestorywithMIK Yes Bhaiya...
Thanks for understanding the solution in simple and easy way😊
I didn't understand line no 21
Please explain
Let's understand it with a simple example :
String str = "we will evlos shtam"
Suppose in the above string I ask you to only reverse "evlos" part and make it "solve"
For that you need the position of "e" and "s" of "evlos"
so, let's say index of "e" is = l
and index of "s" is = r
Because, the function reverse(beginning_position, end_position) requires the beginning and end of something that you want to reverse.
so position for "e" will be = str.begin() + l;
and position of "s" will be = str.begin() + r;
So, we pass these two positions to reverse() function
Then it will only reverse "evlos" and make it "solve"
So our str will now look like = "we will solve shtam"
I hope that helps you and I was able to explain it well.
Let me know if there is any further doubt.
Thanks a lot for watching 😇
Tqsm for this explanation
,
Hello sir ....I had a question. Will an interviewer all us to use inbuilt reverse stl during an interview?
It totally depends.
You can ask the interviewer if you can use Stl or not.
If they ask to implement your own, then you will have to implement.
Aapke leetcode mai dark theme nhi hai kya?
In my current videos, i now use dark theme ❤️
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
n is the length of the input string s.
Your resultant string has a different length.
So in the last line just replace n with result.length()
@@codestorywithMIK while(ss>>token){
if (!result.empty()) {
result = token + " " + result;
}
else {
result = token;
}
}
Time complexity of this code 0(n^2) ?
no its O(N)
@@anshumaan1024 time complexity of swapping two variables is O(1) because we create a temp variable and shuffle the values among the two variables and temp. Now reversing will swap characters from each corresponding ends ie forward and reverse until we reach middle so O(1) xO(N/2) which is O(N/2) which is basically O(N) and not O(N²)
@@rajkumarroul7306 TC will be O(N*M), where N is size of given string, M is average length of words in given string :), now i have understood it
@@anshumaan1024 no it will be O(N) only assume you are reversing a 11 letter sentence which has 2 ,5 letter words time complexity of reversing the whole would be O(11) and reversing each words would be O(5+5+1) ;1 is for traversing space.Bascally O(11) for reversing the whole sentence and O(11) for reversing each words which is O(N)+O(N) or O(2N ) or which is simplified O(N)
@@anshumaan1024 you are reversing each words after u have reversed the whole sentence and not that you are reversing each words after each swap/reverse of sentence so O(N)+O(N) =O(N) and not O(N*M)
s[r]=' ' ku kra