Not the fastest solution for this easy LC problem but it’s readable and easy to understand so I’m fine with this.
class Solution {
fun removeDuplicates(S: String): String {
tailrec fun removeDuplicates_(word: String): String{
var found = false
lateinit var newWord: String
for(index in 1 until word.length) {
if(word[index] == word[index - 1]){
found = true
newWord = word.substring(0, index - 1).plus(word.substring(index + 1, word.length))
break
}
}
return if(found) removeDuplicates_(newWord) else word
}
return removeDuplicates_(S)
}
}
Runtime: 424 ms, faster than 22.22% of Kotlin online submissions for Remove All Adjacent Duplicates In String.
Memory Usage: 36.8 MB, less than 100.00% of Kotlin online submissions for Remove All Adjacent Duplicates In String.