Kotlin Leetcode - 2325. Decode the Message
class Solution {
fun decodeMessage(key: String, message: String): String {
}
}
解題思路
這一題考的是對字串的處理
會需要用到 HashMap
Kotlin 參考解答
class Solution {
fun decodeMessage(key: String, message: String): String {
val code = HashMap<Char, Char>()
code[' '] = ' '
var localKey = key.toSet()
.joinToString("")
.filter { it != ' ' }
localKey.forEachIndexed { i, _ ->
code[localKey[i]] = ('a'..'z').toList()[i]
}
var result = StringBuilder()
message.forEachIndexed { i, _ ->
result.append(code[message[i]])
}
return result.toString()
}
}
回到 leetcode 列表