Last active
April 5, 2021 16:13
-
-
Save dariodiaz/962336c9a57999a33455734a62419f6f to your computer and use it in GitHub Desktop.
[Reverse vowels] Leetcode challenge - Reverse vowels #leetcode #javascript #codinginterview
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
var reverseVowels = function (s) { | |
const arr = s.split(""); | |
let left = 0, right = arr.length; | |
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']; | |
while (left < right) { | |
if (vowels.indexOf(arr[left]) === -1) { | |
left++; | |
continue; | |
} | |
if (vowels.indexOf(arr[right]) === -1) { | |
right--; | |
continue; | |
} | |
const temp = arr[left]; | |
arr[left] = arr[right]; | |
arr[right] = temp; | |
left++; | |
right--; | |
} | |
return arr.join(""); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment