g0001_0100.s0038_count_and_say.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 g0001_0100.s0038_count_and_say
// #Medium #Top_Interview_Questions #String #2022_09_18_Time_317_ms_(41.11%)_Space_35.3_MB_(85.56%)
class Solution {
fun countAndSay(n: Int): String {
if (n == 1) {
return "1"
}
val res = StringBuilder()
val prev = countAndSay(n - 1)
var count = 1
for (i in 1 until prev.length) {
if (prev[i] == prev[i - 1]) {
count++
} else {
res.append(count).append(prev[i - 1])
count = 1
}
}
res.append(count).append(prev[prev.length - 1])
return res.toString()
}
}