length of last word leetcode | leetcode 58 | string

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


#String #Word #Length #last_word)_length #Coding #Programming #Interview #Practice #September #Leetcode #Easy #58 #Algorithm #DataStructure #Java #Preparation #NG #nickode #CookCodeTravel #CCT
Рекомендации по теме
Комментарии
Автор

Very nice explanation sir thankyou so much 🙏🏼🙏🏼🙏🏼
But in split() method, why two or more last spaces of String are removed
Please explain sir how splits method exactly works.

someshsahu
Автор

class Solution {
public int lengthOfLastWord(String s) {
int n = s.length();
while (n > 0 && s.charAt(n - 1) == ' ') n--;
int i = n - 1;
int count = 0;
while (i >= 0 && s.charAt(i) != ' ') {
count++;
i--;
}
return count;
}
}

quandingleberry
Автор

Can Anyone one help me out "Joan Smith" or "Matthew Joan Smith" how I will get only first and last word from these string.Answer must be : - for "Joan Smith" it will be (JS) and for "Matthew Joan Smith" it will be (MS).

singhvimlesh
join shbcf.ru