LeetCode in Kotlin

2947. Count Beautiful Substrings I

Medium

You are given a string s and a positive integer k.

Let vowels and consonants be the number of vowels and consonants in a string.

A string is beautiful if:

Return the number of non-empty beautiful substrings in the given string s.

A substring is a contiguous sequence of characters in a string.

Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.

Consonant letters in English are every letter except vowels.

Example 1:

Input: s = “baeyh”, k = 2

Output: 2

Explanation: There are 2 beautiful substrings in the given string.

You can see that string “aeyh” is beautiful as vowels == consonants and vowels * consonants % k == 0.

You can see that string “baey” is beautiful as vowels == consonants and vowels * consonants % k == 0.

It can be shown that there are only 2 beautiful substrings in the given string.

Example 2:

Input: s = “abba”, k = 1

Output: 3

Explanation: There are 3 beautiful substrings in the given string.

It can be shown that there are only 3 beautiful substrings in the given string.

Example 3:

Input: s = “bcdf”, k = 1

Output: 0

Explanation: There are no beautiful substrings in the given string.

Constraints:

Solution

class Solution {
    fun beautifulSubstrings(s: String, k: Int): Int {
        val numVowels = IntArray(s.length + 1)
        for (i in s.indices) {
            val c = s[i]
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                numVowels[i + 1] = numVowels[i] + 1
            } else {
                numVowels[i + 1] = numVowels[i]
            }
        }
        var step = 1
        while (step < k) {
            if ((step * step) % k == 0) {
                break
            }
            step++
        }
        step *= 2
        var count = 0
        for (i in s.indices) {
            var j = i + step
            while (j <= s.length) {
                if ((numVowels[j] - numVowels[i]) * 2 == j - i) {
                    count++
                }
                j += step
            }
        }
        return count
    }
}