반응형
hi
long time no see.
happy new year..? hahahah
actually, I have solved the algorithm almost every day, but there was not enough time to explain it.
but for starting? happy new year, I bring the new problem. hahaha
I know this is a easy problem. but it is not to me..
but i haven't gave up, and finally I made it.


and this is my solution.
for helping undestanding, I worte the explanation line by line.
var reverseVowels = function(s) {
//key point :
// 1. finding some vowels
//2. reverse them only all the vowels.
// first, I need to check the vowels
//so I will use includes method and new stirng which has whole vowels.
let a = "aeiouAEIOU"
//this is for memorizing the elements of vowels.
let c = []
//and string can't re-assign new element, so I will use array for switching the element.
let d = s.split('')
//for changing the element on reverse order, I will use for loop as reversley
for(let i = 0; i < s.length + 1; i++){
if(a.includes(s[s.length - i])){
c.push(s[s.length - i])
}
}
let z = 0
// this part is for switching the vowels on revers way.
// the point is that vowel should be changed in order.
//so I declared the new value z for checking the order.
for(let i = 0; i < s.length; i++){
if(a.includes(s[i])){
d[i] = c[z]
z++
}
}
return d.join('')
};
I'm just starting to study algorithms; I couldn't even begin before.
But now, I can consider how to solve problems and write my own code, even if it's not perfect or doesn't fully solve the problem.
Compared to the past, my skills have improved.
And I know I will keep getting better and better.
반응형
'열정가득한 개발자의 이야기 > Javascript Algorithm' 카테고리의 다른 글
leet code 409. Longest Palindrome (0) | 2025.01.18 |
---|---|
leet code 387. First Unique Character in a String (javascript) (1) | 2025.01.16 |
geeksforgeeks (Sum of elements in a matrix / basic) (2) | 2024.11.11 |
Palindrome Array (geeksforgeeks_Basic) (3) | 2024.11.07 |
leet code 189. Rotate Array (javascript) (1) | 2024.11.03 |