Skip to content

Commit

Permalink
vowelStrings
Browse files Browse the repository at this point in the history
  • Loading branch information
masx200 committed Jun 17, 2023
1 parent d30607e commit dc06595
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
27 changes: 26 additions & 1 deletion count-vowel-strings-in-ranges/index.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,30 @@ package com.github.masx200.leetcode_test.count_vowel_strings_in_ranges
class Solution {
fun vowelStrings(words: Array<String>, queries: Array<IntArray>): 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())
}
}

fun booleanToInt(b: Boolean) = if (b) 1 else 0
}
3 changes: 1 addition & 2 deletions leetcode-test.iml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
<module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<sourceFolder url="file://$MODULE_DIR$/count-vowel-strings-in-ranges" isTestSource="false" packagePrefix="com.github.masx200.leetcode_test.count_vowel_strings_in_ranges" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" packagePrefix="com.github.masx200.leetcode_test" />
<sourceFolder url="file://$MODULE_DIR$/zigzag-iterator" isTestSource="false" packagePrefix="com.github.masx200.leetcode_test.zigzag_iterator" />
<sourceFolder url="file://$MODULE_DIR$/count-vowel-strings-in-ranges" isTestSource="false" packagePrefix="com.github.masx200.leetcode_test.count_vowel_strings_in_ranges" />
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="com.github.masx200.leetcode_test" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
</component>
Expand Down

0 comments on commit dc06595

Please sign in to comment.