Detect Capital - LeetCode

문제접근🤔


놓쳤던 부분😅


코드😁


14 MB

0 ms

class Solution {
    func detectCapitalUse(_ word: String) -> Bool {
        let wordLength = word.length
        var count = 0
        
        for letter in word {
            if (letter >= "A" && letter <= "Z")
            {
                count += 1
            }
        }
        switch count {
            case 0:
                return (true)
            case 1:
                if (word[word.startIndex] >= "A" && word[word.startIndex] <= "Z")
                {
                    return (true)
                }
                return (false)
            case wordLength:
                return (true)
            default:
                return (false)
        }
    }
}
//다른 사람 풀이
class Solution {
    func detectCapitalUse(_ word: String) -> Bool {
        var count = 0
        for ch in word where ch.isUppercase { count += 1 }
        switch count {
        case 0, word.count:
            return true
        case 1:
            return word.first!.isUppercase
        default:
            return false
        }
    }
}