0520. Detect Capital

# 520. Detect Capital#

## 题目 #

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

All letters in this word are capitals, like “USA”.

All letters in this word are not capitals, like “leetcode”.

Only the first letter in this word is capital, like “Google”.

Given a string word, return true if the usage of capitals in it is right.

Example 1:

``````Input: word = "USA"
Output: true
``````

Example 2:

``````Input: word = "FlaG"
Output: false
``````

Constraints:

• 1 <= word.length <= 100
• word consists of lowercase and uppercase English letters.

## 解题思路 #

• 把 word 分别转换为全部小写 wLower，全部大写 wUpper，首字母大写的字符串 wCaptial
• 判断 word 是否等于 wLower, wUpper, wCaptial 中的一个，如果是返回 true，否则返回 false

## 代码 #

``````
package leetcode

import "strings"

func detectCapitalUse(word string) bool {
wLower := strings.ToLower(word)
wUpper := strings.ToUpper(word)
wCaptial := strings.ToUpper(string(word[0])) + strings.ToLower(string(word[1:]))
if wCaptial == word || wLower == word || wUpper == word {
return true
}
return false
}

``````

Apr 8, 2023