Reverse Vowels of a String
- javascript
- typescript
- string
- two pointers
Problem URL:Reverse Vowels of a String
My Solution
JavaScript / TypeScript
const reverseVowels = (s: string): string => {
const vowelAsciiMap: Record<string, number> = {
a: 97,
e: 101,
i: 105,
o: 111,
u: 117
};
let i = 0,
j = s.length - 1,
reversed = s.split("");
while (i < j) {
let leftCharInMap = reversed[i].toLowerCase() in vowelAsciiMap,
rightCharInMap = reversed[j].toLowerCase() in vowelAsciiMap;
if (leftCharInMap && rightCharInMap) {
[reversed[i], reversed[j]] = [reversed[j], reversed[i]];
i += 1;
j -= 1;
} else if (!leftCharInMap) {
i += 1;
} else if (!rightCharInMap) {
j -= 1;
}
}
return reversed.join("");
};
Let's Connect
Twitter •GitHub •LinkedIn