Detect Capital

The easiest way to solve this problem is to count the number of uppercase character. We can easily do that by using map and Character.isUpperCase functions.

class Solution {
    fun detectCapitalUse(word: String): Boolean {
        return word.map { if(Character.isUpperCase(it)) 1 else 0 }.sum().let {
            return it == word.length || 
                it == 1 && Character.isUpperCase(word[0]) || 
                it == 0    
        }
        
    }
}
Runtime: 160 ms, faster than 52.00% of Kotlin online submissions for Detect Capital.
Memory Usage: 32.7 MB, less than 100.00% of Kotlin online submissions for Detect Capital.

According to the description, there are 3 criterias to determine if the word is using the capitalization correctly:

  1. All are capital
  2. First character is capital
  3. Zero capital characters

These criterias can be easily represented by the sum of all uppercase characters:

sum == word.length || sum == 1 && Character.isUpperCase(word[0]) || sum == 0