diff --git a/solution/2500-2599/2559.Count Vowel Strings in Ranges/README.md b/solution/2500-2599/2559.Count Vowel Strings in Ranges/README.md index b76611b1ba688..34809aeb921bd 100644 --- a/solution/2500-2599/2559.Count Vowel Strings in Ranges/README.md +++ b/solution/2500-2599/2559.Count Vowel Strings in Ranges/README.md @@ -306,15 +306,29 @@ func vowelStrings(words []string, queries [][]int) []int { ```ts function vowelStrings(words: string[], queries: number[][]): number[] { const vowels = new Set(['a', 'e', 'i', 'o', 'u']); - const n = words.length; - const s: number[] = new Array(n + 1).fill(0); - for (let i = 0; i < n; ++i) { - if (vowels.has(words[i][0]) && vowels.has(words[i][words[i].length - 1])) { - s[i + 1] = s[i] + 1; - } else { - s[i + 1] = s[i]; - } - } + const s = new Array(words.length + 1).fill(0); + + words.forEach((w, i) => { + const x = +(vowels.has(w[0]) && vowels.has(w.at(-1)!)); + s[i + 1] = s[i] + x; + }); + + return queries.map(([l, r]) => s[r + 1] - s[l]); +} +``` + +#### JavaScript + +```js +function vowelStrings(words, queries) { + const vowels = new Set(['a', 'e', 'i', 'o', 'u']); + const s = new Array(words.length + 1).fill(0); + + words.forEach((w, i) => { + const x = +(vowels.has(w[0]) && vowels.has(w.at(-1))); + s[i + 1] = s[i] + x; + }); + return queries.map(([l, r]) => s[r + 1] - s[l]); } ``` diff --git a/solution/2500-2599/2559.Count Vowel Strings in Ranges/README_EN.md b/solution/2500-2599/2559.Count Vowel Strings in Ranges/README_EN.md index 1645999adeb40..a5aa935989e80 100644 --- a/solution/2500-2599/2559.Count Vowel Strings in Ranges/README_EN.md +++ b/solution/2500-2599/2559.Count Vowel Strings in Ranges/README_EN.md @@ -304,15 +304,29 @@ func vowelStrings(words []string, queries [][]int) []int { ```ts function vowelStrings(words: string[], queries: number[][]): number[] { const vowels = new Set(['a', 'e', 'i', 'o', 'u']); - const n = words.length; - const s: number[] = new Array(n + 1).fill(0); - for (let i = 0; i < n; ++i) { - if (vowels.has(words[i][0]) && vowels.has(words[i][words[i].length - 1])) { - s[i + 1] = s[i] + 1; - } else { - s[i + 1] = s[i]; - } - } + const s = new Array(words.length + 1).fill(0); + + words.forEach((w, i) => { + const x = +(vowels.has(w[0]) && vowels.has(w.at(-1)!)); + s[i + 1] = s[i] + x; + }); + + return queries.map(([l, r]) => s[r + 1] - s[l]); +} +``` + +#### JavaScript + +```js +function vowelStrings(words, queries) { + const vowels = new Set(['a', 'e', 'i', 'o', 'u']); + const s = new Array(words.length + 1).fill(0); + + words.forEach((w, i) => { + const x = +(vowels.has(w[0]) && vowels.has(w.at(-1))); + s[i + 1] = s[i] + x; + }); + return queries.map(([l, r]) => s[r + 1] - s[l]); } ``` diff --git a/solution/2500-2599/2559.Count Vowel Strings in Ranges/Solution2.js b/solution/2500-2599/2559.Count Vowel Strings in Ranges/Solution2.js new file mode 100644 index 0000000000000..6ace0b29fb9a3 --- /dev/null +++ b/solution/2500-2599/2559.Count Vowel Strings in Ranges/Solution2.js @@ -0,0 +1,11 @@ +function vowelStrings(words, queries) { + const vowels = new Set(['a', 'e', 'i', 'o', 'u']); + const s = new Array(words.length + 1).fill(0); + + words.forEach((w, i) => { + const x = +(vowels.has(w[0]) && vowels.has(w.at(-1))); + s[i + 1] = s[i] + x; + }); + + return queries.map(([l, r]) => s[r + 1] - s[l]); +} diff --git a/solution/2500-2599/2559.Count Vowel Strings in Ranges/Solution2.ts b/solution/2500-2599/2559.Count Vowel Strings in Ranges/Solution2.ts index 6f3f88314a224..f734a6be09245 100644 --- a/solution/2500-2599/2559.Count Vowel Strings in Ranges/Solution2.ts +++ b/solution/2500-2599/2559.Count Vowel Strings in Ranges/Solution2.ts @@ -1,13 +1,11 @@ function vowelStrings(words: string[], queries: number[][]): number[] { const vowels = new Set(['a', 'e', 'i', 'o', 'u']); - const n = words.length; - const s: number[] = new Array(n + 1).fill(0); - for (let i = 0; i < n; ++i) { - if (vowels.has(words[i][0]) && vowels.has(words[i][words[i].length - 1])) { - s[i + 1] = s[i] + 1; - } else { - s[i + 1] = s[i]; - } - } + const s = new Array(words.length + 1).fill(0); + + words.forEach((w, i) => { + const x = +(vowels.has(w[0]) && vowels.has(w.at(-1)!)); + s[i + 1] = s[i] + x; + }); + return queries.map(([l, r]) => s[r + 1] - s[l]); }