From d82d476920b8cd14aa3d3b9b7ecca84eac175134 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 9 Jun 2024 14:28:39 +0800 Subject: [PATCH] 139th Commit --- package.json | 3 +- .../reverseVowels.bench.ts | 16 ++++++++ .../reverseVowels.spec.ts | 8 ---- .../reverseVowels.test.ts | 13 ++++++ .../reverseVowels.ts | 40 +++++++++++++++++++ 5 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 src/page-4/345. Reverse Vowels of a String/reverseVowels.bench.ts delete mode 100644 src/page-4/345. Reverse Vowels of a String/reverseVowels.spec.ts create mode 100644 src/page-4/345. Reverse Vowels of a String/reverseVowels.test.ts diff --git a/package.json b/package.json index 3310f75..a41be27 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "scripts": { "lint": "biome check --apply ./", "check": "tsc --noEmit", - "test": "vitest --dir src --run" + "test": "vitest run", + "bench": "vitest bench --run" }, "devDependencies": { "@biomejs/biome": "^1.7.3", diff --git a/src/page-4/345. Reverse Vowels of a String/reverseVowels.bench.ts b/src/page-4/345. Reverse Vowels of a String/reverseVowels.bench.ts new file mode 100644 index 0000000..ebb9206 --- /dev/null +++ b/src/page-4/345. Reverse Vowels of a String/reverseVowels.bench.ts @@ -0,0 +1,16 @@ +import { bench } from 'vitest'; + +import { reverseVowels, reverseVowels2 } from './reverseVowels'; + +describe('345. Reverse Vowels of a String', () => { + // fastest + bench('reverseVowels', () => { + reverseVowels('hello'); + reverseVowels('leetcode'); + }); + + bench('reverseVowels2', () => { + reverseVowels2('hello'); + reverseVowels2('leetcode'); + }); +}); diff --git a/src/page-4/345. Reverse Vowels of a String/reverseVowels.spec.ts b/src/page-4/345. Reverse Vowels of a String/reverseVowels.spec.ts deleted file mode 100644 index 667d4b0..0000000 --- a/src/page-4/345. Reverse Vowels of a String/reverseVowels.spec.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { reverseVowels } from './reverseVowels'; - -describe('345. Reverse Vowels of a String', () => { - it('reverseVowels', () => { - expect(reverseVowels('hello')).toBe('holle'); - expect(reverseVowels('leetcode')).toBe('leotcede'); - }); -}); diff --git a/src/page-4/345. Reverse Vowels of a String/reverseVowels.test.ts b/src/page-4/345. Reverse Vowels of a String/reverseVowels.test.ts new file mode 100644 index 0000000..9b28384 --- /dev/null +++ b/src/page-4/345. Reverse Vowels of a String/reverseVowels.test.ts @@ -0,0 +1,13 @@ +import { reverseVowels, reverseVowels2 } from './reverseVowels'; + +describe('345. Reverse Vowels of a String', () => { + test('reverseVowels', () => { + expect(reverseVowels('hello')).toBe('holle'); + expect(reverseVowels('leetcode')).toBe('leotcede'); + }); + + test('reverseVowels2', () => { + expect(reverseVowels2('hello')).toBe('holle'); + expect(reverseVowels2('leetcode')).toBe('leotcede'); + }); +}); diff --git a/src/page-4/345. Reverse Vowels of a String/reverseVowels.ts b/src/page-4/345. Reverse Vowels of a String/reverseVowels.ts index 8c07cb5..727724d 100644 --- a/src/page-4/345. Reverse Vowels of a String/reverseVowels.ts +++ b/src/page-4/345. Reverse Vowels of a String/reverseVowels.ts @@ -1,5 +1,8 @@ type ReverseVowels = (s: string) => string; +/** + * Accepted + */ export const reverseVowels: ReverseVowels = (s) => { // Helper function to check if a character is a vowel function isVowel(char: string): boolean { @@ -34,3 +37,40 @@ export const reverseVowels: ReverseVowels = (s) => { // Convert the array back to a string and return it return chars.join(''); }; + +/** + * Accepted + */ +export const reverseVowels2: ReverseVowels = (s) => { + // Helper function to check if a character is a vowel + function isVowel(char: string): boolean { + return 'aeiouAEIOU'.includes(char); + } + + // Extract all vowels from the string + const vowels: string[] = []; + + for (const char of s) { + if (isVowel(char)) vowels.push(char); + } + + // Reverse the list of vowels + vowels.reverse(); + + // Reconstruct the string, replacing vowels with reversed vowels + const result: string[] = []; + + let vowelIndex = 0; + + for (const char of s) { + if (isVowel(char)) { + result.push(vowels[vowelIndex]); + vowelIndex += 1; + } else { + result.push(char); + } + } + + // Convert the array back to a string and return it + return result.join(''); +};