Skip to content

Commit dc06595

Browse files
committed
vowelStrings
1 parent d30607e commit dc06595

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

count-vowel-strings-in-ranges/index.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,30 @@ package com.github.masx200.leetcode_test.count_vowel_strings_in_ranges
33
class Solution {
44
fun vowelStrings(words: Array<String>, queries: Array<IntArray>): IntArray {
55

6+
7+
val prefixs = IntArray(words.size) { 0 }
8+
9+
words.forEachIndexed { i, s ->
10+
if (i == 0) {
11+
12+
prefixs[i] = booleanToInt(isVowelString(s))
13+
} else {
14+
prefixs[i] = prefixs[i - 1] + booleanToInt(isVowelString(s))
15+
16+
}
17+
18+
}
19+
return IntArray(queries.size) {
20+
val li = queries[it][0]
21+
val ri = queries[it][1]
22+
if (li == ri) booleanToInt(isVowelString(words[li])) else prefixs[ri] - if (li == 0) 0 else prefixs[li - 1]
23+
}
24+
}
25+
26+
private fun isVowelString(s: String): Boolean {
27+
val vowels = hashSetOf('a', 'e', 'i', 'o', 'u')
28+
return s.isNotEmpty() && vowels.contains(s[0]) && vowels.contains(s.last())
629
}
7-
}
30+
31+
fun booleanToInt(b: Boolean) = if (b) 1 else 0
32+
}

leetcode-test.iml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
<module version="4">
33
<component name="AdditionalModuleElements">
44
<content url="file://$MODULE_DIR$" dumb="true">
5+
<sourceFolder url="file://$MODULE_DIR$/count-vowel-strings-in-ranges" isTestSource="false" packagePrefix="com.github.masx200.leetcode_test.count_vowel_strings_in_ranges" />
56
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" packagePrefix="com.github.masx200.leetcode_test" />
67
<sourceFolder url="file://$MODULE_DIR$/zigzag-iterator" isTestSource="false" packagePrefix="com.github.masx200.leetcode_test.zigzag_iterator" />
7-
<sourceFolder url="file://$MODULE_DIR$/count-vowel-strings-in-ranges" isTestSource="false" packagePrefix="com.github.masx200.leetcode_test.count_vowel_strings_in_ranges" />
8-
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="com.github.masx200.leetcode_test" />
98
<excludeFolder url="file://$MODULE_DIR$/target" />
109
</content>
1110
</component>

0 commit comments

Comments
 (0)