[LeetCode#58]Length of Last Word

Problem:

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.

Note: A word is defined as a character sequence consists of non-space characters only.

For example, 
Given s = "Hello World",
return 5.

Analysis:

This is very typical problem of after count. (only at the next step, we know wheather the previous count should be recorded)
For this problem, we can't determine if the current character is the last character of a word. We can only make such decision when the we know the next character is boundary or space character ' '. 

For this kind of problem, we need to carefully design the following things:
problem 1. the start condition, since we need to maintain a count, what's the intial value of the count?
problem 2. how do we decide to record a qualifed word? when do we update the global record? 

For problem 1, for this problem, there could be empty space at any place, even at s.charAt(0), we should only make a count when we are sure the current character is not empty space. Thus the intial value of count should be 0.

For problem 2, the instant idea is to use ' ' as a sign to record previous word's information.
if (s.charAt(i) == ' ') {
    len = count;
    count = 0;
}
It looks simple, but it could incur complex logic problem. 
1. What if there are multiple ' ' after a word?
"jay      "
What would you do?
Increase the checking over wheather the empty space's previous character is ' ' ? (why? too complex)

2. What if there are no empty space after a word?
"jay"

Those two uncertainties could make our code ugly and hard to maintain, Can we simplify it?
Yes. Since empty space is just a way of separating word, it could appear at any indexes and multi times, Why should we care about it?

3. How about use following checking condition?
3.1. If the current character is not ' ' and, it is previous character is ' ', apparenlty we should record the previous word's length.
3.2  If the current character is not ' ', and it is previous character is not ' ', apparenlty we are still in a word, we should just increase the count.
if (s.charAt(i) != ' ') {
    if (i == 0 || s.charAt(i-1) != ' ') {
        count++;
    } else{
        len = count;
        count = 1;
    } 
} else{
    continue;
}

Note:
a. since when i is 0, it has no previous character, we have:
if (i == 0 || s.charAt(i-1) != ' ') {
    count++;
} 
b. After counting must consider the case the last word was not counted.
for (int i = 0; i < s.length(); i++) {
    ...
}
len = count;

Solution:

public class Solution {
    public int lengthOfLastWord(String s) {
        if (s == null)
            throw new IllegalArgumentException("s is null");
        int len = 0;
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ' ') {
                if (i == 0 || s.charAt(i-1) != ' ') {
                    count++;
                } else{
                    len = count;
                    count = 1;
                } 
            } else{
                continue;
            }
        }
        len = count;
        return len;
    }
}
原文地址:https://www.cnblogs.com/airwindow/p/4793410.html