Skip to content

Commit

Permalink
problem: new problem solution - 345 . Reverse Vowels of a String
Browse files Browse the repository at this point in the history
  • Loading branch information
squxq committed Sep 10, 2023
1 parent a6cc5db commit d65aca5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ My approach to solving LeetCode problems typically involves the following steps:
| 1071 | [Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) | Algorithms | [TypeScript](./problems/algorithms/greatestCommonDivisorOfStrings/GreatestCommonDivisorOfStrings.ts) | Easy |
| 1431 | [Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/) | Algorithms | [TypeScript](./problems/algorithms/kidsWithTheGreatestNumberOfCandies/KidsWithTheGreatestNumberOfCandies.ts) | Easy |
| 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) | Algorithms | [TypeScript](./problems/algorithms/canPlaceFlowers/CanPlaceFlowers.ts) | Easy |
| 345 | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | Algorithms | [TypeScript](./problems/algorithms/reverseVowelsOfAString/ReverseVowelsOfAString.ts) | Easy |
| ... | ... | ... | ... | ... |

In this table:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Source : https://leetcode.com/problems/reverse-vowels-of-a-string/
// Author : squxq
// Date : 2023-09-10

import { reverseVowels } from "./ReverseVowelsOfAString";

describe("reverseVowels", () => {
it("should reverse vowels in a string with lowercase vowels", () => {
expect(reverseVowels("hello")).toBe("holle");
expect(reverseVowels("leetcode")).toBe("leotcede");
expect(reverseVowels("a")).toBe("a");
expect(reverseVowels("aeiou")).toBe("uoiea");
});

it("should reverse vowels in a string with uppercase vowels", () => {
expect(reverseVowels("HELLO")).toBe("HOLLE");
expect(reverseVowels("LEETCODE")).toBe("LEOTCEDE");
expect(reverseVowels("A")).toBe("A");
expect(reverseVowels("AEIOU")).toBe("UOIEA");
});

it("should handle strings with no vowels", () => {
expect(reverseVowels("bcdfg")).toBe("bcdfg");
expect(reverseVowels("XYZ")).toBe("XYZ");
expect(reverseVowels("123")).toBe("123");
expect(reverseVowels("")).toBe("");
});

it("should handle strings with only vowels", () => {
expect(reverseVowels("aeiou")).toBe("uoiea");
expect(reverseVowels("AEIOU")).toBe("UOIEA");
});

it("should handle strings with mixed characters", () => {
expect(reverseVowels("h3ll0")).toBe("h3ll0");
expect(reverseVowels("L33tC0d3")).toBe("L33tC0d3");
expect(reverseVowels("vowels AEIOU here")).toBe("vowels UOIEA here");
});

it("should handle long strings (within constraints)", () => {
const longString = "a".repeat(300000);
const reversedLongString = "u".repeat(300000);
expect(reverseVowels(longString)).toBe(reversedLongString);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Source : https://leetcode.com/problems/reverse-vowels-of-a-string/
// Author : squxq
// Date : 2023-09-10

/*****************************************************************************************************
*
* Given a string s, reverse only all the vowels in the string and return it.
*
* The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more
* than once.
*
* Example 1:
* Input: s = "hello"
* Output: "holle"
* Example 2:
* Input: s = "leetcode"
* Output: "leotcede"
*
* Constraints:
*
* 1 <= s.length <= 3 * 10^5
* s consist of printable ASCII characters.
******************************************************************************************************/

export function reverseVowels(s: string): string {
const vowels = new Set<string>([
"a",
"e",
"i",
"o",
"u",
"A",
"E",
"I",
"O",
"U",
]);
const result: string[] = s.split("");
let left: number = 0;
let right: number = result.length - 1;
while (left < right) {
while (left < right && !vowels.has(result[left] as string)) {
left++;
}

while (left < right && !vowels.has(result[right] as string)) {
right--;
}

if (left < right) {
const temp: string = result[left] as string;
result[left] = result[right] as string;
result[right] = temp;

left++;
right--;
}
}
return result.join("");
}

0 comments on commit d65aca5

Please sign in to comment.