diff --git a/count-vowel-strings-in-ranges/index.kt b/count-vowel-strings-in-ranges/index.kt index 82f1a4fa..ad1d64d9 100644 --- a/count-vowel-strings-in-ranges/index.kt +++ b/count-vowel-strings-in-ranges/index.kt @@ -3,5 +3,30 @@ package com.github.masx200.leetcode_test.count_vowel_strings_in_ranges class Solution { fun vowelStrings(words: Array, queries: Array): IntArray { + + val prefixs = IntArray(words.size) { 0 } + + words.forEachIndexed { i, s -> + if (i == 0) { + + prefixs[i] = booleanToInt(isVowelString(s)) + } else { + prefixs[i] = prefixs[i - 1] + booleanToInt(isVowelString(s)) + + } + + } + return IntArray(queries.size) { + val li = queries[it][0] + val ri = queries[it][1] + if (li == ri) booleanToInt(isVowelString(words[li])) else prefixs[ri] - if (li == 0) 0 else prefixs[li - 1] + } + } + + private fun isVowelString(s: String): Boolean { + val vowels = hashSetOf('a', 'e', 'i', 'o', 'u') + return s.isNotEmpty() && vowels.contains(s[0]) && vowels.contains(s.last()) } -} \ No newline at end of file + + fun booleanToInt(b: Boolean) = if (b) 1 else 0 +} diff --git a/leetcode-test.iml b/leetcode-test.iml index 152b8ad4..bfaedc45 100644 --- a/leetcode-test.iml +++ b/leetcode-test.iml @@ -2,10 +2,9 @@ + - -