class Solution { public int lengthOfLastWord(String s) { s = s.stripTrailing(); int emptyIndex = s.lastIndexOf(' '); return s.length() - emptyIndex - 1; } }
Using the STL stringstream , we can easily get the output for this question : class Solution { public: int lengthOfLastWord(string s) { stringstream ss(s); string token; string last; while( ss >> token){ last = token; } return last.size(); } };
Sir without using npos it gives right answer but sir when we subtract lastSpaceIdx from len then -1 then answer must be less than length of the string but it gives right answer as length of the string for the case when there is no space in the string.
2035. Partition Array Into Two Arrays to Minimize Sum Difference:--->please make a tutorila on this ,i have watched more than 10 videos but not able to understand...i request you...
@@EB-ot8uu class Solution { public: int lengthOfLastWord(string s) { stringstream ss(s); string word; int l = 0; while (ss >> word) { l = word.length(); } return l; } };
Aproach 1 🤯🤯 Mai kitna hard tarikhe se kiya tha. int lengthOfLastWord(string s) { int n = s.size(); int lastCount=0; int currentCount = 0; for(int i=0; i
int length = 0; bool counting = false; for (int i = s.length() - 1; i >= 0; i--) { if (s[i] != ' ') { counting = true; length++; } else if (counting) { break; // will it terminate if the space encountered after last word and result [0] } } return length;
your java code for 2nd approach give wrong answers. public int lengthOfLastWord(String s){ s=s.trim(); int lastSpace =s.lastIndexOf(' '); return lastSpace==-1?s.length():s.length()-lastSpace-1; } this code is passed all testcase.🎉❤
I'm 2023 passout btech cse(Tier 2) and don't get a good placement so i'm too confused whether i should give CAT or GATE for better career growth(I don't know whether my interest lies in coding or anything else). Can you please help me in choosing a right career path 🥲
Day 400 completed of yours,Mine 28 ,all bcz of you bhai
congratulations on 38k . you deserve millions.
Amazing ❤❤❤❤❤
Bhai biweekly contest ka 4th(3098. Find the Sum of Subsequence Powers) waale pe video banau na
ha bhai bnao ispe
@@kapilnitb kab se contest de rha tu
@@devmadaan5146 last week se start kiya h
Congratulations🤩
much easier approach using stringstream...beats 100%
class Solution {
public:
int lengthOfLastWord(string s) {
stringstream ss(s);
string word;
string lastWord;
while (ss >> word) {
lastWord = word;
}
return lastWord.length();
}
};
Congratulations 🙌
sir please cover the question : Minimum Window Subsequence
Bhaiya ..can you please post the gfg POTD also . 🙏❤
please make video on (leetcode 310) minimum height trees
class Solution {
public int lengthOfLastWord(String s) {
s = s.stripTrailing();
int emptyIndex = s.lastIndexOf(' ');
return s.length() - emptyIndex - 1;
}
}
bro i learn new method
Thanks a lot bhaiya ❤❤ Congrats for 38k subs ❤❤
Stl solution helping sir 🙌
Congratulations on 38K Subs 🎉
congrats man. soon 50k
Using the STL stringstream , we can easily get the output for this question :
class Solution {
public:
int lengthOfLastWord(string s) {
stringstream ss(s);
string token;
string last;
while( ss >> token){
last = token;
}
return last.size();
}
};
Indeed 🙌
Sir without using npos it gives right answer but sir when we subtract lastSpaceIdx from len then -1 then answer must be less than length of the string but it gives right answer as length of the string for the case when there is no space in the string.
string::npos (which is the maximum value representable by size_t, typically equivalent to -1)
kya haal chaal bhai !! mere bolne pe hi "you can watch in 1.5x daala h na"
3097. Shortest Subarray With OR at Least K II Please Provide the Solution of this too ❤❤
bhaiya agar time mile to weekly and biweekly contest ke q4 kara dena dono acche question hai, i was able to solve 4th in biweekly but go tle :(
2035. Partition Array Into Two Arrays to Minimize Sum Difference:--->please make a tutorila on this ,i have watched more than 10 videos but not able to understand...i request you...
int lengthOfLastWord(string s) {
istringstream iss(s);
string word;
vector words;
while(iss>>word){
words.push_back(word);
}
if (words.empty()){
return 0;
}
else return words.back().size();
} is this good approach
bhaiya ji bina spos kere bhi toh chl rha lc pe
why stl is not used in interview
Bhaiya Good Morning, DP ka playlist kab complete hoga?
8 bje bhaith gya tha padhne !! sahi h bc
reverse while loop skip indexing
then next while loop count length if char is present i.e s[i] != ' '
sir aapne bola tha 50k hone par you will tell us how you started this channel. bhool mat jana . also face reveal kardo ab
😂😂
int lengthOfLastWord(string s) {
int n=s.size();
int len=0;
bool found=false;
for(int i=n-1; i>=0; i--){
if(s[i]!=' '){
len++;
found=true;
}
else if(found==true){
break;
}
}
return len;
}
solved by stringstream
can you please share your code bro.
@@EB-ot8uu
class Solution {
public:
int lengthOfLastWord(string s) {
stringstream ss(s);
string word;
int l = 0;
while (ss >> word) {
l = word.length();
}
return l;
}
};
@@bhuppidhamii wow. where can I learn more about stringstream
in YT there are plenty of videos + there are a lot of doc in gfg's as well@@gui-codes
Aproach 1 🤯🤯 Mai kitna hard tarikhe se kiya tha.
int lengthOfLastWord(string s) {
int n = s.size();
int lastCount=0;
int currentCount = 0;
for(int i=0; i
int length = 0;
bool counting = false;
for (int i = s.length() - 1; i >= 0; i--) {
if (s[i] != ' ') {
counting = true;
length++;
}
else if (counting) {
break; // will it terminate if the space encountered after last word and result [0]
}
}
return length;
your java code for 2nd approach give wrong answers.
public int lengthOfLastWord(String s){
s=s.trim();
int lastSpace =s.lastIndexOf(' ');
return lastSpace==-1?s.length():s.length()-lastSpace-1;
}
this code is passed all testcase.🎉❤
I'm 2023 passout btech cse(Tier 2) and don't get a good placement so i'm too confused whether i should give CAT or GATE for better career growth(I don't know whether my interest lies in coding or anything else). Can you please help me in choosing a right career path 🥲
how much coding you have done?
@@lofireverbz-wy7go 700+ questions all together 🥺