Advertisement

Encrypt and Decrypt Strings - LeetCode 2227 Solution

Encrypt and Decrypt Strings - Complete Solution Guide

Encrypt and Decrypt Strings is LeetCode problem 2227, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. A string is encrypted with the following 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 stri

Detailed Explanation

The problem requires designing a data structure that can encrypt and decrypt strings based on given keys, values, and a dictionary. The encryption process replaces characters in a string with corresponding 2-character strings from the 'values' array, based on their mapping in the 'keys' array. The decryption process reverses this, but with the possibility of multiple valid decryptions. The goal is to implement an `Encrypter` class with `encrypt` and `decrypt` methods. The `encrypt` method encrypts a given string. The `decrypt` method counts how many possible decryptions of a given string exist within a given 'dictionary'.

Solution Approach

The solution pre-computes the encrypted strings of all the dictionary words and stores their counts in a hash map (or similar data structure). During encryption, it iterates through the input string, replacing each character with its corresponding two-character value from the 'values' array, if a mapping exists. During decryption, it simply returns the number of times the input string appears in the pre-computed encrypted words.

Step-by-Step Algorithm

  1. Step 1: Initialization: In the constructor, create a hash map to store the key-value mappings. Also, create another hash map to store the counts of encrypted dictionary words.
  2. Step 2: Encryption Preparation: Iterate through the dictionary words. For each word, encrypt it using the key-value mappings. If the encrypted word is not empty (meaning the original word contains only chars present in keys), add the encrypted word to the hash map tracking the count of each word.
  3. Step 3: Encryption: In the `encrypt` function, iterate through the input string. For each character, look up its corresponding value in the hash map. If the character is not found, return an empty string. Otherwise, append the corresponding value to the result string.
  4. Step 4: Decryption: In the `decrypt` function, simply return the count of the input string from the encrypted words hash map. If the encrypted word is not found in the hashmap, return 0.

Key Insights

  • Insight 1: Pre-compute and store encrypted words from the dictionary during initialization to optimize the `decrypt` function. This avoids repeated encryption calculations.
  • Insight 2: Use a hash map (dictionary in Python, HashMap in Java, unordered_map in C++) to store the mappings between keys and values for efficient lookups during encryption.
  • Insight 3: The `decrypt` function needs to count the number of valid decryptions, but since the dictionary is fixed, we can simply encrypt the dictionary words and store the counts. This drastically improves the decryption operation.

Complexity Analysis

Time Complexity: O(N*M)

Space Complexity: O(D*M)

Topics

This problem involves: Array, Hash Table, String, Design, Trie.

Companies

Asked at: Duolingo.