LeetCode – Length of Last Word (Java)

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0.

Java Solution

Very simple question. We just need a flag to mark the start of letters from the end. If a letter starts and the next character is not a letter, return the length.

public int lengthOfLastWord(String s) { 
    if(s==null || s.length() == 0)
        return 0;
 
    int result = 0;
    int len = s.length();
 
    boolean flag = false;
    for(int i=len-1; i>=0; i--){
        char c = s.charAt(i);
        if((c>='a' && c<='z') || (c>='A' && c<='Z')){
            flag = true;
            result++;
        }else{
            if(flag)
                return result;
        }
    }
 
    return result;
}
Category >> Algorithms >> Interview  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • neha patwardhan

    public int lengthOfLastWord(String s) {
    int count = 0;
    boolean flag = false;
    for(int i=s.length()-1; i>=0;i–){
    if(s.charAt(i)== ‘ ‘ && !flag){
    continue;
    }
    if(s.charAt(i)==’ ‘ && flag) break;
    count++;
    flag=true;
    }
    return count;
    }

  • CG

    The if statement in the for-loop needs to be updated to include a hyphenated last word.

    if((c>=’a’ && c=’A’ && c<='Z') || c == '-')

  • Eugene Arnatovich


    public int solution(String s) {
    if (s == null) {
    return 0;
    }
    s = s.trim();
    if (s.length() == 0) {
    return 0;
    }
    String[] words = s.split("\s");
    return words[words.length-1].length();
    }

  • James

    public class Solution {

    public int lengthOfLastWord(String s) {

    if (s.length() == 0 || s == null)

    return 0;

    int total_len = s.length();

    int lastWordLen = 0;

    for (int i = total_len – 1; i >= 0; i–) {

    char c = s.charAt(i);

    if ((c >= ‘a’ && c = ‘A’ && c 0)

    return lastWordLen;

    }

    }

    return lastWordLen;

    }

    }

  • NB****


    void FunctionRunner::FindLastWordSize_Test() {
    string str = "My Last word is Harry";
    size_t pos = str.find_last_of(" ");
    if (pos == string::npos) {
    cout << "Last word size: " << 0 << "n";
    } else {
    cout << "Last word size: " << str.length() - pos - 1 << "n";
    }
    }

  • Vimukthi Weerasiri

    public int lengthOfLastWord(String s) {
    int lastSpace = 0;
    s = s.trim();
    for (int i = 0; i < s.length() ; i++) {
    if (s.charAt(i) == ' ') lastSpace = i + 1;
    }
    return s.length() – lastSpace;
    }

  • Ecch IOni

    You just doubled the space used :), but honestly in real world we all would do it because it’s faster to type.

  • jensiepoo

    public int lengthOfLastWord(String s) {
    s = s.trim();
    String[] arr = s.split(” “);

    if(s.length() == 0){
    return 0;
    }
    else{
    return arr[arr.length-1].length();
    }
    }