kt: add «2375. Construct Smallest Number From DI String»
URL: https://leetcode.com/problems/construct-smallest-number-from-di-string/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
c849923112
commit
4473b4b6eb
1 changed files with 28 additions and 0 deletions
28
kt/construct-smallest-number-from-di-string.kt
Normal file
28
kt/construct-smallest-number-from-di-string.kt
Normal file
|
@ -0,0 +1,28 @@
|
|||
class Solution {
|
||||
private fun buildSequence(
|
||||
pattern: CharArray,
|
||||
result: StringBuilder,
|
||||
index: Int,
|
||||
count: Int,
|
||||
): Int {
|
||||
var nextCount = count
|
||||
when {
|
||||
index >= pattern.size -> {}
|
||||
pattern[index] == 'I' -> buildSequence(pattern, result, index + 1, index + 1)
|
||||
else -> {
|
||||
nextCount = buildSequence(pattern, result, index + 1, count)
|
||||
}
|
||||
}
|
||||
|
||||
result.append(nextCount + 1)
|
||||
return nextCount + 1
|
||||
}
|
||||
|
||||
fun smallestNumber(pattern: String): String =
|
||||
StringBuilder().let { result ->
|
||||
buildSequence(pattern.toCharArray(), result, 0, 0)
|
||||
|
||||
result.reverse()
|
||||
result.toString()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue