LeetCode 520. Detect Capital
日檢N1合格,開心開心。
題目
檢查字串是否符合以下規則:
- 全大寫
- 全小寫
- 字首大寫其他小寫
解法
字串長度N,計算大寫字元數cnt:
- cnt==N
- cnt==0
- cnt==1且字首大寫
class Solution:
def detectCapitalUse(self, word: str) -> bool:
cnt = sum(['A' <= c <= 'Z' for c in word])
return cnt == 0 or cnt == len(word) or cnt == 1 and 'A' <= word[0] <= 'Z'