![JAR search and dependency download from the Maven repository](/logo.png)
g2001_2100.s2075_decode_the_slanted_ciphertext.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 g2001_2100.s2075_decode_the_slanted_ciphertext
// #Medium #String #Simulation #2023_06_26_Time_418_ms_(100.00%)_Space_49.2_MB_(100.00%)
class Solution {
fun decodeCiphertext(encodedText: String, rows: Int): String {
if (rows == 1) {
return encodedText
}
val total = encodedText.length
val cols = total / rows
val grid = Array(rows) { CharArray(cols) }
var index = 0
for (i in 0 until rows) {
for (j in 0 until cols) {
grid[i][j] = encodedText[index++]
}
}
val sb = StringBuilder()
var colIndex = 0
while (colIndex < cols) {
var j = colIndex
var i = 0
while (j < cols && i < rows) {
sb.append(grid[i][j])
j++
i++
}
colIndex++
}
var i = sb.length - 1
while (i >= 0 && sb[i] == ' ') {
i--
}
return sb.substring(0, i + 1)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy