Best C# Solution


  • 0
    K
        public int LengthOfLastWord(string s) 
        {
            bool start = false;
            int count = 0;
            
            for(int i = s.Length - 1; i >= 0; i--){
                if (!start && s[i] != ' ')
                    start = true;
                if (start && s[i] == ' ')
                    return count;
                if (start && s[i] != ' '){
                    count++;
                }
            }
            
            return count;
        }
    }

Log in to reply
 

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