![JAR search and dependency download from the Maven repository](/logo.png)
g0901_1000.s0917_reverse_only_letters.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0901_1000.s0917_reverse_only_letters
// #Easy #String #Two_Pointers #2023_04_16_Time_126_ms_(100.00%)_Space_33.6_MB_(94.44%)
class Solution {
fun reverseOnlyLetters(s: String): String {
val array = s.toCharArray()
var i = 0
var j = array.size - 1
while (i < j) {
if (Character.isLetter(array[i]) && Character.isLetter(array[j])) {
val temp = array[i]
array[i++] = array[j]
array[j--] = temp
} else if (Character.isLetter(array[i])) {
j--
} else if (Character.isLetter(array[j])) {
i++
} else {
i++
j--
}
}
return String(array)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy