good explanation here is my code without using trim() method class Solution { public int lengthOfLastWord(String s) { int count=0; if(s==null|| s.length()==0) return 0; int i = s.length()-1; while(i>=0 && s.charAt(i)==' ') { i--; // we are skiping the end(tail) spaces } while(i>=0 && s.charAt(i)!=' ') { count++; i--; } return count; } }
// remove heading and trailing spaces from string // split string to whilespaces string[] strArray = s.Trim().Split(' '); // get last items of string array return strArray[strArray.Length-1].Length; } Runtime: 46 ms, faster than 91.33% of C# online submissions for Length of Last Word.
class Solution { public int lengthOfLastWord(String s) { int i=s.length()-1; int count=0; for (;i>=0;i--){ if (s.charAt(i)==' ') continue; else break; } for (;i>=0;i--){ if (s.charAt(i)!=' '){ count+=1; } else break; } return count; } }
String st-" Hello world leet. "; String [] str=st.split("\\s"); SOP(str[str.length-1]. length ()); Is this correct, instead of reverse and break please share your thoughts
good explanation
here is my code without using trim() method
class Solution {
public int lengthOfLastWord(String s) {
int count=0;
if(s==null|| s.length()==0) return 0;
int i = s.length()-1;
while(i>=0 && s.charAt(i)==' ')
{
i--; // we are skiping the end(tail) spaces
}
while(i>=0 && s.charAt(i)!=' ')
{
count++;
i--;
}
return count;
}
}
Super pa good explanation ❤
Nice Explanation
Every Queen need a crown and here is your crown 👑
public int LengthOfLastWord(string s) {
// remove heading and trailing spaces from string
// split string to whilespaces
string[] strArray = s.Trim().Split(' ');
// get last items of string array
return strArray[strArray.Length-1].Length;
}
Runtime: 46 ms, faster than 91.33% of C# online submissions for Length of Last Word.
great explanation ma'am!!
mam LinkedIn link is not working...
U explained very easily
Please share your code series of java
Madam please add some more question to the playlist
class Solution {
public int lengthOfLastWord(String s) {
int i=s.length()-1;
int count=0;
for (;i>=0;i--){
if (s.charAt(i)==' ') continue;
else break;
}
for (;i>=0;i--){
if (s.charAt(i)!=' '){
count+=1;
}
else break;
}
return count;
}
}
👍👍
Thanks!!
Can you share the git repo for the codes which you do
Hello,
Will share soon, the GIT repo.
String st-" Hello world leet. ";
String [] str=st.split("\\s");
SOP(str[str.length-1]. length ());
Is this correct, instead of reverse and break please share your thoughts
Yes..this is also correct way to solve this problem
No need to use the variable count. Return i-1 when i==' '.
trim may not work;
as an alternate we can write replace break; with
else{
if(count>0)
return count;
}
Explained very well
Luffy is still joyboy
Hi mam
class Solution {
public int lengthOfLastWord(String s) {
int c=0,k=0;
for(int i=s.length()-1;i>=0;i--)
{
if(s.charAt(i)==' ')
c++;
else
break;
}
for(int i=s.length()-1-c;i>=0;i--)
{
if(s.charAt(i)==' ')
break;
else
k++;
}
return k;
}
}