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:
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 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
#coding=utf-8
class Solution(object):
def detectCapitalUse(self, word):
"""
:type word: str
:rtype: bool
"""
if word[0].isupper() and word[1:].lower() == word[1:]:
return True
elif word.isupper():
return True
elif word.islower():
return True
else:
return False
本文介绍了一个算法,该算法能够判断一个单词的大小写使用是否正确。正确的大小写格式包括:全部字母大写、全部字母小写或仅首字母大写(如果单词超过一个字母)。文章还提供了一个Python实现示例。
414

被折叠的 条评论
为什么被折叠?



