-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2559.count-vowel-strings-in-ranges.ts
65 lines (60 loc) · 1.6 KB
/
2559.count-vowel-strings-in-ranges.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// @leet start
function checkIfValid(word: string) {
const vovels = ["a", "e", "i", "o", "u"];
return vovels.includes(word[0]) && vovels.includes(word[word.length - 1]);
}
function prefixSolution(words: string[], queries: number[][]): number[] {
const result: number[] = [];
let sum = 0;
const prefixArray: number[] = [];
for (let i = 0; i < words.length; i++) {
const elm = words[i];
const isValid = checkIfValid(elm);
if (isValid) sum++;
prefixArray.push(sum);
}
for (let i = 0; i < queries.length; i++) {
const elm = queries[i];
const left = elm[0];
const right = elm[1];
const ans =
left === 0
? prefixArray[right]
: prefixArray[right] - prefixArray[left - 1];
result.push(ans);
}
return result;
}
function mapSolution(words: string[], queries: number[][]): number[] {
const table = new Map<string, boolean>();
const result: number[] = [];
queries.forEach((element) => {
const start = element[0];
const end = element[1];
let i = start;
let numOfVovelStr = 0;
while (i <= end) {
const word = words[i];
if (table.has(word)) {
if (table.get(word) === true) {
numOfVovelStr++;
}
} else {
const isValid = checkIfValid(word);
if (isValid) {
numOfVovelStr++;
table.set(word, true);
} else {
table.set(word, false);
}
}
i++;
}
result.push(numOfVovelStr);
});
return result;
}
function vowelStrings(words: string[], queries: number[][]): number[] {
return prefixSolution(words, queries);
}
// @leet end