diff --git a/301-400/345. Reverse Vowels of a String.md b/301-400/345. Reverse Vowels of a String.md new file mode 100644 index 0000000..0a59b55 --- /dev/null +++ b/301-400/345. Reverse Vowels of a String.md @@ -0,0 +1,59 @@ +# 345. Reverse Vowels of a String + +- Difficulty: Easy. +- Related Topics: Two Pointers, String. +- Similar Questions: Reverse String, Remove Vowels from a String, Faulty Keyboard and Sort Vowels in a String. + +## Problem + +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` + + +## Solution +```bash +/** + * @param {string} s + * @return {string} + */ +var reverseVowels = function(s) { + let start = 0, end = s.length - 1; + let vowel = "aeiouAEIOU"; + s = s.split('') + + while(start < end) + { + while(start < end && !vowel.includes(s[start])) + { + start ++; + } + + while(start < end && !vowel.includes(s[end])) + { + end--; + } + + let temp = s[start]; + s[start] = s[end]; + s[end] = temp; + + start++; + end--; + } + + return s.join(''); + +};