-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
problem: new problem solution - 345 . Reverse Vowels of a String
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
problems/algorithms/reverseVowelsOfAString/ReverseVowelsOfAString.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
problems/algorithms/reverseVowelsOfAString/ReverseVowelsOfAString.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
} |