Skip to content

Commit

Permalink
139th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 9, 2024
1 parent bcde19d commit d82d476
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions src/page-4/345. Reverse Vowels of a String/reverseVowels.bench.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});

This file was deleted.

13 changes: 13 additions & 0 deletions src/page-4/345. Reverse Vowels of a String/reverseVowels.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
40 changes: 40 additions & 0 deletions src/page-4/345. Reverse Vowels of a String/reverseVowels.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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('');
};

0 comments on commit d82d476

Please sign in to comment.