Length of Last Word (LeetCode 58) | Full solution with examples | Easiest Method

preview_player
Показать описание

Chapters:
00:00 - Intro
00:38 - Problem Statement and Description
02:38 - Brute Force Method
05:10 - A very simple solution
06:48 - Dry-run of Code
09:41 - Final Thoughts

📚 Links to topics I talk about in the video:

📖 Reference Books:

🎥 My Recording Gear:

💻 Get Social 💻

#leetcode #programming #interview
Рекомендации по теме
Комментарии
Автор

I solved this using String.trim() function .

Maneeshce
Автор

Your explanation is very good and easy to grasp love your videos ❤️ keep growing

prateektiwari
Автор

Does it work if the input is a single character

sibik
Автор

int lengthOfLastWord(string s) {
int i=0;
int ans;
for(i=0;i<s.length();){
while(s[i]==' ') i++;
int j=0;
while(s[i]!=' ' and i<s.length()){
i++;
j++;
}
if(j!=0) ans=j;
}
return ans;
}

fffooccc
Автор

public int lengthOfLastWord(String s) {


int count=0;
for(int i=s.length()-1;i>=0;i--)
{
if(s.charAt(i)!=' ')
{
count=count+1;
}
else if(s.charAt(i)==' '&&count>=1)
{
break;
}

}

return count;


}
} enjoy bachoooo

anubhav.codess
Автор

String[] arr = str.split(" ");

String lastWord = arr[arr.length-1];
return lastWord.length();
what about this code

kandusobanbabu
Автор

Thank you Nikhil, Can you just explain me how
else{
if(count>0)
return count;
}
this works??

jagratsahoo
Автор

I'm asking you to make a videos hoe to input a tree, how to output a tree, most common trees in CP

instedlar
Автор

Thanks, could you make a video of Leetcode 22. "Generate Parentheses" problem solution with DP?

dduvitv
Автор

But input is single word how to find bro

venkatkrishnan
Автор

can we use trim() method na two remove the spaces

pavankumarpadamati