[LeetCode] 45. Reverse Vowels of a String(Kotlin)

0

LeetCode

목록 보기
9/58
post-thumbnail

[LeetCode] 45. Reverse Vowels of a String(Kotlin)

풀이

class Solution {
    fun reverseVowels(s: String): String {
        val vowels = setOf('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')

        var l = 0
        var r = s.length -1
        var ans = StringBuilder(s)
        while(l < r){
            val isLVowel = s[l] in vowels
            val isRVowel = s[r] in vowels 
            if(isLVowel && isRVowel){
                // swap vowels
                ans.set(l, s[r])
                ans.set(r, s[l])
            }
            else if(isLVowel){
                r--
                continue
            }
            else if(isRVowel){
                l++
                continue
            }
            l++
            r--
            
        }
        return ans.toString()
    }
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글