LeetCode in Kotlin

3136. Valid Word

Easy

A word is considered valid if:

You are given a string word.

Return true if word is valid, otherwise, return false.

Notes:

Example 1:

Input: word = “234Adas”

Output: true

Explanation:

This word satisfies the conditions.

Example 2:

Input: word = “b3”

Output: false

Explanation:

The length of this word is fewer than 3, and does not have a vowel.

Example 3:

Input: word = “a3$e”

Output: false

Explanation:

This word contains a '$' character and does not have a consonant.

Constraints:

Solution

class Solution {
    fun isValid(word: String): Boolean {
        if (word.length < 3) {
            return false
        }
        var hasVowel = false
        var hasConsonant = false
        for (c in word.toCharArray()) {
            if (Character.isLetter(c)) {
                val ch = c.lowercaseChar()
                if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                    hasVowel = true
                } else {
                    hasConsonant = true
                }
            } else if (!Character.isDigit(c)) {
                return false
            }
        }
        return hasVowel && hasConsonant
    }
}