알고리즘/LeetCode
[LeetCode/easy] Reverse Vowels of a String
녕이
2022. 8. 12. 23:42
728x90
https://leetcode.com/problems/reverse-vowels-of-a-string/
Reverse Vowels of a String - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
투 포인터를 사용해서 앞 뒤로 l, r 포인터를 두고
- 둘 다 모음인 경우 swap, l++, r--
- 둘 다 자음인 경우 l++, r--
- 왼쪽만 자음 l++
- 오른쪽만 자음 r--
bool isVowel(char c){
if(c == 'a' || c == 'e' || c == 'o' || c == 'i' || c == 'u'
|| c == 'A' || c == 'E' || c == 'O' || c == 'I' || c == 'U') return true;
return false;
}
string reverseVowels(string s) {
string answer = s;
int l = 0, r = s.size()-1;
while(l<r){
if(isVowel(s[l]) && isVowel(s[r])){
swap(s[l], s[r]);
l++; r--;
}
else if(!isVowel(s[l]) && isVowel(s[r])) l++;
else if(isVowel(s[l]) && !isVowel(s[r])) r--;
else {
l++; r--;
}
}
answer = s;
return answer;
}
728x90