DAY 27
0

# 520. Detect Capital

## Question

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

### Example1

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

### Example2

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

### Constraints

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

## 解題

• 字母全大寫
• 字母全小寫
• 字母只有字首大寫

### Think

• 先把第一位的字母抓出來判斷字首是大寫還小寫。
• 如果是大寫:
• 代表剩下的字母必須都是大寫或都是小寫
• 減少跑的迴圈次數，從頭跟從尾往中間判斷是不是都是大寫或都是小寫
• 如果是小寫:
• 代表剩下的字母必須都是小寫
• 一樣從頭跟從尾往中間判斷是不是都是小寫

### Code

#### Python

``````class Solution:
def isPalindrome(self, x: int) -> bool:
x = str(x)
left = 0
right = len(x)-1

while left < right:
if x[left] != x[right]:
return False

left += 1
right -= 1

return True
``````

### Result

• Python

30天 Leetcode解題之路30