Encrypt and Decrypt Strings - Problem

You are given a character array keys containing unique characters and a string array values containing strings of length 2. You are also given another string array dictionary that contains all permitted original strings after decryption.

You should implement a data structure that can encrypt or decrypt a 0-indexed string.

Encryption Process:

  • For each character c in the string, we find the index i satisfying keys[i] == c in keys.
  • Replace c with values[i] in the string.

Note that in case a character of the string is not present in keys, the encryption process cannot be carried out, and an empty string "" is returned.

Decryption Process:

  • For each substring s of length 2 occurring at an even index in the string, we find an i such that values[i] == s. If there are multiple valid i, we choose any one of them. This means a string could have multiple possible strings it can decrypt to.
  • Replace s with keys[i] in the string.

Implement the Encrypter class:

  • Encrypter(char[] keys, String[] values, String[] dictionary) Initializes the Encrypter class with keys, values, and dictionary.
  • String encrypt(String word1) Encrypts word1 with the encryption process described above and returns the encrypted string.
  • int decrypt(String word2) Returns the number of possible strings word2 could decrypt to that also appear in dictionary.

Input & Output

Example 1 — Basic Encryption/Decryption
$ Input: keys = ['a', 'b', 'c'], values = ["hj", "xy", "wz"], dictionary = ["ab", "bc"], operations = [["encrypt", "ab"], ["decrypt", "hjxy"]]
Output: ["hjxy", "1"]
💡 Note: Encrypt 'ab': 'a'→"hj", 'b'→"xy", result "hjxy". Decrypt "hjxy": splits to "hj"+"xy" which could be 'a'+'b'="ab", and "ab" exists in dictionary, so count is 1.
Example 2 — Multiple Dictionary Matches
$ Input: keys = ['a', 'b'], values = ["hj", "hj"], dictionary = ["aa", "bb"], operations = [["decrypt", "hjhj"]]
Output: ["2"]
💡 Note: Decrypt "hjhj": both 'a' and 'b' map to "hj", so "hjhj" could decode to "aa" or "bb". Both exist in dictionary, so count is 2.
Example 3 — Invalid Encryption
$ Input: keys = ['a', 'b'], values = ["hj", "xy"], dictionary = ["ab"], operations = [["encrypt", "ac"]]
Output: [""]
💡 Note: Encrypt 'ac': 'a'→"hj" but 'c' is not in keys array, so encryption fails and returns empty string.

Constraints

  • 1 ≤ keys.length == values.length ≤ 26
  • values[i].length == 2
  • 1 ≤ dictionary.length ≤ 100
  • 1 ≤ dictionary[i].length ≤ 100
  • At most 200 calls will be made to encrypt and decrypt

Visualization

Tap to expand
Encrypt and Decrypt Strings INPUT keys: 'a' 'b' 'c' values: "hj" "xy" "wz" Mapping: 'a' --> "hj" 'b' --> "xy" 'c' --> "wz" dictionary: "ab" "bc" operations: ["encrypt","ab"] ["decrypt","hjxy"] ALGORITHM STEPS 1 Pre-encrypt Dictionary Encrypt all dict words upfront "ab" --> "hjxy" "bc" --> "xywz" 2 Build Count Map Count encrypted --> original "hjxy": 1, "xywz": 1 (maps encrypted to count) 3 Encrypt Operation Replace chars with values "ab" --> "hj"+"xy" = "hjxy" 4 Decrypt = Lookup Count Return count from map decrypt("hjxy") = map["hjxy"] = 1 FINAL RESULT Operation 1: encrypt("ab") 'a' --> "hj" 'b' --> "xy" Result: "hjxy" Operation 2: decrypt("hjxy") Lookup in pre-encrypted map count["hjxy"] = 1 Result: "1" Output Array: ["hjxy", "1"] OK - Both operations complete Key Insight: Pre-encrypted Dictionary Approach Instead of trying all possible decryptions (exponential), pre-encrypt all dictionary words and count them. Decryption becomes O(1) lookup: just return the count of valid original strings that encrypt to the given encrypted string. This avoids TLE on large inputs! TutorialsPoint - Encrypt and Decrypt Strings | Pre-encrypted Dictionary Approach
Asked in
Google 35 Amazon 28 Microsoft 22
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen