https://leetcode.com/problems/reverse-vowels-of-a-string/description/ Easy

Решение

class Solution {
    companion object {
        private val vowels = BooleanArray(128).apply {
            this['a'.code] = true
            this['e'.code] = true
            this['i'.code] = true
            this['o'.code] = true
            this['u'.code] = true
            this['A'.code] = true
            this['E'.code] = true
            this['I'.code] = true
            this['O'.code] = true
            this['U'.code] = true
        }
    }

    fun reverseVowels(s: String): String {
        val chars = s.toCharArray()
        var i = 0
        var j = chars.lastIndex
        while (i < j) {
            while (i < j && !isVowel(chars[i])) i++
            while (i < j && !isVowel(chars[j])) j--
            if (i < j) {
                val t = chars[i]
                chars[i] = chars[j]
                chars[j] = t
                i++
                j--
            }
        }
        return String(chars)
    }

    private fun isVowel(c: Char): Boolean {
        val code = c.code
        return code < 128 && vowels[code]
    }
}