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

g2201_2300.s2227_encrypt_and_decrypt_strings.Encrypter.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g2201_2300.s2227_encrypt_and_decrypt_strings

// #Hard #Array #String #Hash_Table #Design #Trie
// #2023_06_27_Time_483_ms_(75.00%)_Space_54.4_MB_(100.00%)

class Encrypter(keys: CharArray, values: Array, dictionary: Array) {
    private val eMap: MutableMap
    private val dMap: MutableMap

    init {
        eMap = HashMap()
        dMap = HashMap()
        for (i in keys.indices) {
            eMap[keys[i]] = values[i]
        }
        for (s in dictionary) {
            val str = encrypt(s)
            if (str != "" && str != "null") {
                dMap[str] = dMap.getOrDefault(str, 0) + 1
            }
        }
    }

    fun encrypt(word1: String): String {
        val sb = StringBuilder()
        for (c in word1.toCharArray()) {
            sb.append(eMap[c])
        }
        return sb.toString()
    }

    fun decrypt(word2: String): Int {
        return dMap.getOrDefault(word2, 0)
    }
}
/*
 * Your Encrypter object will be instantiated and called as such:
 * var obj = Encrypter(keys, values, dictionary)
 * var param_1 = obj.encrypt(word1)
 * var param_2 = obj.decrypt(word2)
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy