-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.kt
42 lines (40 loc) · 1.3 KB
/
Solution.kt
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
32
33
34
35
36
37
38
39
40
41
42
/**
* Created by Inno Fang on 2017/12/30.
*/
class Solution {
fun detectCapitalUse(word: String): Boolean {
return when (word) {
word.toUpperCase() -> true
word.toLowerCase() -> true
else -> {
val first = word[0].toString()
if (word.length > 1 && first == first.toUpperCase()) {
val last = word.substring(1)
if (last == last.toLowerCase()) return true
}
return false
}
}
}
}
class Solution2 {
fun detectCapitalUse(word: String): Boolean {
if (word.equals(word.toUpperCase())) return true
if (word.equals(word.toLowerCase())) return true
val first = word[0].toString()
if (first.equals(first.toUpperCase())) {
if (word.length > 1) {
val last = word.substring(1)
if (last.equals(last.toLowerCase())) return true
}
}
return false
}
}
fun main(args: Array<String>) {
Solution().detectCapitalUse("USA").let(::println)
Solution().detectCapitalUse("solution").let(::println)
Solution().detectCapitalUse("Google").let(::println)
Solution().detectCapitalUse("FlaG").let(::println)
Solution().detectCapitalUse("FLAG").let(::println)
}