Encrypt and Decrypt Strings - Problem
π Encrypt and Decrypt Strings
You're building a custom encryption system that can both encrypt and decrypt strings using a special mapping scheme. This system is unique because decryption might have multiple valid interpretations!
Given:
- A
keysarray of unique characters (the alphabet of your cipher) - A
valuesarray of 2-character strings (the encrypted representations) - A
dictionaryarray of valid words that can result from decryption
Encryption Process:
For each character c in the input string:
- Find the index
iwherekeys[i] == c - Replace
cwithvalues[i] - If any character isn't found in keys, return
""
Decryption Process:
For each 2-character substring at even positions:
- Find which
values[i]matches this substring - Replace it with
keys[i] - Count how many possible decryptions appear in the dictionary
Example: If keys = ['a', 'b'] and values = ["01", "10"], then "ab" encrypts to "0110" and "0110" could decrypt back to "ab" (if "ab" is in the dictionary).
Input & Output
example_1.py β Basic Encryption and Decryption
$
Input:
keys = ['a', 'b', 'c'], values = ["ei", "zf", "ei"], dictionary = ["abcdefghijklmnop"]
Operations:
- encrypt("abcdefghijklmnop")
- decrypt("eizfeiei")
βΊ
Output:
encrypt: "eizfeiei"
decrypt: 1
π‘ Note:
Encryption maps each character using the keys array: 'a'β'ei', 'b'β'zf', 'c'β'ei', etc. The string "abcdefghijklmnop" becomes "eizfeiei...". For decryption, "eizfeiei" can decode back to words that appear in the dictionary, and there's 1 such word.
example_2.py β Multiple Possible Decryptions
$
Input:
keys = ['a', 'b'], values = ["01", "10"], dictionary = ["ab", "ba"]
Operations:
- encrypt("ab")
- decrypt("0110")
βΊ
Output:
encrypt: "0110"
decrypt: 2
π‘ Note:
Both "ab" and "ba" encrypt to "0110" since 'a'β'01' and 'b'β'10'. When decrypting "0110", it can be split as "01"+"10" (giving "ab") and both words are in the dictionary, so the count is 2.
example_3.py β Character Not in Keys
$
Input:
keys = ['a', 'b'], values = ["01", "10"], dictionary = ["abc"]
Operations:
- encrypt("abc")
- decrypt("01")
βΊ
Output:
encrypt: ""
decrypt: 0
π‘ Note:
The character 'c' is not in the keys array, so encryption of "abc" fails and returns empty string. "01" has odd length so cannot be properly decrypted (each encrypted char is 2 digits), resulting in 0 possible decryptions.
Visualization
Tap to expand
Understanding the Visualization
1
Build Code Book
Create hash maps: charβcode and codeβ[possible chars]
2
Preprocess Dictionary
Encrypt all approved words and store their counts
3
Encrypt Message
For each character, lookup its code in O(1) time
4
Decrypt Message
Lookup encrypted string in preprocessed dictionary counts
Key Takeaway
π― Key Insight: By preprocessing the dictionary (encrypting all words during initialization), we transform the decrypt operation from exponential backtracking to a simple O(1) hash table lookup, making the system blazingly fast for real-time encryption and decryption operations.
Time & Space Complexity
Time Complexity
O(1) encrypt, O(1) decrypt after O(D*L) preprocessing
D is dictionary size, L is average word length. Each operation is O(1) after preprocessing
β Linear Growth
Space Complexity
O(K + D*L)
K for key-value mappings, D*L for storing encrypted dictionary words
β Linear Space
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
- keys contains unique characters
- All characters in dictionary[i] are in keys
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code