728x90
문제
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, 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.
class Solution {
func detectCapitalUse(_ word: String) -> Bool {
// all letter in capital
if word == word.uppercased(){
return true
}
//all letter are not capital
if word == word.lowercased(){
return true
}
//first letter capital
if String(word.first!) == String(word.first!).uppercased(){
var tmp = word
tmp.removeFirst()
if tmp == tmp.lowercased(){
return true
}
}
return false
}
}
728x90
'Algorithm > LeetCode' 카테고리의 다른 글
LeetCode - Largest Time for Given Digits (0) | 2020.09.01 |
---|---|
LeetCode - Running Sum of 1d Array (0) | 2020.08.30 |
💯 Daily LeetCode Challenge Day_20 - Remove Linked List Elements (2) | 2020.07.29 |
💯 Daily LeetCode Challenge Day_19 - Add Binary (0) | 2020.07.29 |
💯 Daily LeetCode Challenge Day_18 - Course Schedule 2 (0) | 2020.07.24 |
댓글