forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectCapital.swift
More file actions
31 lines (26 loc) · 842 Bytes
/
DetectCapital.swift
File metadata and controls
31 lines (26 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Question Link: https://leetcode.com/problems/detect-capital/
* Primary idea: Counts uppercased characters then compare to the standards.
*
* Time Complexity: O(n), Space Complexity: O(1)
*
*/
class DetectCapital {
func detectCapitalUse(_ word: String) -> Bool {
var capitalNum = 0, isFirstUpperCased = false
for char in word.characters {
if char.isUpperCased() {
capitalNum += 1
}
}
if let firstChar = word.characters.first {
isFirstUpperCased = firstChar.isUpperCased()
}
return capitalNum == 0 || (capitalNum == 1 && isFirstUpperCased) || capitalNum == word.characters.count
}
}
fileprivate extension Character {
func isUpperCased() -> Bool {
return String(self).uppercased() == String(self)
}
}