Simple Java Solution


  • 0
    F
    public class Solution {
        public int lengthOfLastWord(String s) {
            String trim = s.trim();
            int last = trim.lastIndexOf(" ");
            if (last < 0) {
                return trim.length();
            } else {
                return Math.abs(trim.length() - last -1);
            }
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.