2025-08-18 17:23:18 +0000 UTC

Decode the Message

Code

class Solution:
    def decodeMessage(self, key: str, message: str) -> str:
        subst = [-1] * 26
        res = []
        count = 0
        for char in key:
            if char == " ":
                continue
            idx = ord(char) - 97
            if subst[idx] == -1:
                subst[idx] = count
                count += 1
        for char in message:
            if char == " ":
                val = char
            else:
                val = chr(subst[ord(char) - 97] + 97)
            res.append(val)
        return "".join(res)