Skip to content

Commit 9ca65f9

Browse files
author
Yi Gu
committed
Add Solution to Detect Capital
1 parent 9eebee1 commit 9ca65f9

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* [Microsoft](#microsoft)
2828

2929
## Progress
30-
[Problem Status](#problem-status) shows the latest progress to all 400+ questions. Currently we have 215 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
30+
[Problem Status](#problem-status) shows the latest progress to all 500+ questions. Currently we have 216 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3131

3232

3333
## Array
@@ -74,6 +74,7 @@
7474
[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)| [Swift](./FizzBuzz.swift)| Easy| O(n)| O(1)|
7575
[Keyboard Row](https://leetcode.com/problems/keyboard-row/)| [Swift](./KeyboardRow.swift)| Easy| O(nm)| O(n)|
7676
[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)| [Swift](./String/ValidPalindrome.swift)| Easy| O(n)| O(n)|
77+
[Detect Capital](https://leetcode.com/problems/detect-capital/)| [Swift](./String/DetectCapital.swift)| Easy| O(n)| O(1)|
7778
[Count and Say](https://leetcode.com/problems/count-and-say/)| [Swift](./String/CountAndSay.swift)| Easy| O(n^2)| O(n)|
7879
[Flip Game](https://leetcode.com/problems/flip-game/)| [Swift](./String/FlipGame.swift)| Easy| O(n)| O(n)|
7980
[Implement strStr()](https://leetcode.com/problems/implement-strstr/)| [Swift](./String/StrStr.swift)| Easy| O(nm)| O(n)|

String/DetectCapital.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/detect-capital/
3+
* Primary idea: Counts uppercased characters then compare to the standards.
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class DetectCapital {
10+
func detectCapitalUse(_ word: String) -> Bool {
11+
var capitalNum = 0, isFirstUpperCased = false
12+
13+
for char in word.characters {
14+
if char.isUpperCased() {
15+
capitalNum += 1
16+
}
17+
}
18+
19+
if let firstChar = word.characters.first {
20+
isFirstUpperCased = firstChar.isUpperCased()
21+
}
22+
23+
return capitalNum == 0 || (capitalNum == 1 && isFirstUpperCased) || capitalNum == word.characters.count
24+
}
25+
}
26+
27+
fileprivate extension Character {
28+
func isUpperCased() -> Bool {
29+
return String(self).uppercased() == String(self)
30+
}
31+
}

0 commit comments

Comments
 (0)