[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值