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.
풀이
/*
[문제]
- 문자열 s가 주어진다.
- 문자열 s의 모든 모음을 반대로 뒤집어서 리턴
- 모음은 대/소문자로 주어짐
*/
const VOWELS_SET = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
var reverseVowels = function (s) {
const vowels = [];
const replacedS = [...s].map((letter) => {
if (VOWELS_SET.has(letter)) {
vowels.push(letter);
return '*';
}
return letter;
});
const reversed = replacedS.map((letter) => {
if (letter === '*') return vowels.pop();
return letter;
});
return reversed.join('');
};
'Algorithm & 자료구조 > 알고리즘 w.JavaScript' 카테고리의 다른 글
151. Reverse Words in a String (0) | 2024.01.03 |
---|---|
[Leetcode] 605. Can Place Flowers (0) | 2023.12.31 |
[Leetcode] 1431. Kids With the Greatest Number of Candies (0) | 2023.12.31 |
[Leetcode] 1071. Greatest Common Divisor of Strings (0) | 2023.12.28 |
[Leetcode] 1768.Merge Strings Alternately (2) | 2023.12.18 |