Kotlin Leetcode - 520. Detect Capital
class Solution {
fun detectCapitalUse(word: String): Boolean {
}
}
解題思路
這一題考的是對字串的處理
處理起來非常的直觀
根據題目的要求
要判斷下列幾個滿足條件
- 大寫字母個數等同所有字母數
- 大寫字母個數等於零
- 大寫字母個數為一,並且第一個字為大寫
Kotlin 參考解答
class Solution {
fun detectCapitalUse(word: String): Boolean {
val numOfCapital = word.filter { it in 'A'..'Z' }.length
val firstCapitalIndex = word.indexOfFirst { it in 'A'..'Z' }
return numOfCapital == word.length
|| numOfCapital == 0
|| (numOfCapital == 1 && firstCapitalIndex == 0)
}
}
回到 leetcode 列表