All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2301_2400.s2325_decode_the_message.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g2301_2400.s2325_decode_the_message

// #Easy #String #Hash_Table #2023_06_30_Time_169_ms_(96.88%)_Space_35.4_MB_(100.00%)

class Solution {
    fun decodeMessage(key: String, message: String): String {
        val sb = StringBuilder()
        val temp: MutableMap = HashMap()
        val alphabet = CharArray(26)
        var itr = 0
        var c = 'a'
        while (c <= 'z') {
            alphabet[c.code - 'a'.code] = c
            ++c
        }
        for (i in key.indices) {
            if (!temp.containsKey(key[i]) && key[i] != ' ') {
                temp[key[i]] = alphabet[itr++]
            }
        }
        for (j in message.indices) {
            if (message[j] == ' ') {
                sb.append(' ')
            } else {
                val result = temp[message[j]]!!
                sb.append(result)
            }
        }
        return sb.toString()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy