题目描述

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital if it has more than one letter, like "Google".

Otherwise, we define that this word doesn't use capitals in a right way.

Example 1:

Input: "USA"
Output: True

Example 2:

Input: "FlaG"
Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

解法一

思路

对于合格的单词,有如下要求:

  1. 字母全为大写,或全为小写。
  2. 仅第一个字母为大写。

从代码的角度来看,我们肯定是从头开始顺序检查每个字母,看是否符合要求;比如“FlaG“,当我们顺序一直检查到”G“时,发现已经有”l“和”a“是小写了,所以这个单词不满足要求。

我们不妨设置3个布尔变量作为哨兵:【已有小写字母】、【已有一个大写字母】和【已有两个大写字母】,其初始之均为False。

这样对于每个字母:

  • 如果是大写:如果【已有小写字母】,则不满足要求;否则更新【已有一个大写字母】或【已有两个大写字母】。
  • 如果是小写:如果【已有两个大写字母】,则不满足要求;否则更新【已有小写字母】。

Python

class Solution(object):
    def detectCapitalUse(self, word):
        """
        :type word: str
        :rtype: bool
        """
        saw_lower = False
        saw_one_upper = False
        saw_two_upper = False
        for letter in word:
            if letter >= 'A' and letter <= 'Z':
                if saw_lower:
                    return False
                if saw_one_upper:
                    saw_two_upper = True
                else:
                    saw_one_upper = True
            if letter >= 'a' and letter <= 'z':
                if saw_two_upper:
                    return False
                saw_lower = True
        return True

Java

public class Solution {
    public boolean detectCapitalUse(String word) {
        boolean sawLower = false;
        boolean sawOneUpper = false;
        boolean sawTwoUpper = false;
        for (char letter : word.toCharArray()) {
            if (letter >= 'A' && letter <= 'Z') {
                if (sawLower) {
                    return false;
                }
                if (sawOneUpper) {
                    sawTwoUpper = true;
                } else {
                    sawOneUpper = true;
                }
            }
            if (letter >= 'a' && letter <= 'z') {
                if (sawTwoUpper) {
                    return false;
                }
                sawLower = true;
            }
        }
        return true;
    }
}

C++

class Solution {
public:
    bool detectCapitalUse(string word) {
        bool sawLower = false;
        bool sawOneUpper = false;
        bool sawTwoUpper = false;
        for (auto letter : word) {
            if (letter >= 'A' && letter <= 'Z') {
                if (sawLower) {
                    return false;
                }
                if (sawOneUpper) {
                    sawTwoUpper = true;
                } else {
                    sawOneUpper = true;
                }
            }
            if (letter >= 'a' && letter <= 'z') {
                if (sawTwoUpper) {
                    return false;
                }
                sawLower = true;
            }
        }
        return true;
    }
};