FastPrepEncrypt and Decrypt Strings

Encrypt and Decrypt Strings

Duolingo logoDuolingo● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Build an encrypter from three constructor inputs:

  • keys[i] is a unique one-character key;
  • values[i] is the two-character encryption for that key; and
  • dictionary contains valid plaintext words.

Process each row [method, word] in operations:

  • encrypt: replace every character of word with its mapped two-character value. If any character has no key, the result is the empty string.
  • decrypt: count dictionary entries whose complete encryption equals word.

Return one string per operation in order. An encryption result is returned directly; a decryption count is returned in decimal form. Different keys may share the same encrypted value.

Function

processEncrypter(keys: String[], values: String[], dictionary: String[], operations: String[][]) → String[]

Examples

Example 1

keys = ["a","b","c","d"]values = ["ei","zf","ei","am"]dictionary = ["abcd","acbd","adbc","badc","dacb","cadb","cbda","abad"]operations = [["encrypt","abcd"],["decrypt","eizfeiam"]]return = ["eizfeiam","2"]

abcd encrypts to eizfeiam. Exactly abcd and abad in the dictionary produce that ciphertext.

Example 2

keys = ["a","b"]values = ["aa","bb"]dictionary = ["ab","ba"]operations = [["encrypt","ac"],["decrypt","aabb"]]return = ["","1"]

The first word contains an unmapped character. Only ab encrypts to aabb.

Constraints

  • 1 <= keys.length == values.length <= 26.
  • Every key is a distinct lowercase English character represented as a one-character string.
  • Every value contains exactly two lowercase English characters; values need not be unique.
  • 1 <= dictionary.length <= 100, and dictionary words contain only lowercase English letters.
  • 1 <= operations.length <= 100.
  • An encrypt word has length at most 2000.
  • A decrypt word has positive even length at most 200.

More Duolingo problems

See Duolingo hiring insights
public String[] processEncrypter(String[] keys, String[] values, String[] dictionary, String[][] operations) {
    // write your code here
}
keys["a","b","c","d"]
values["ei","zf","ei","am"]
dictionary["abcd","acbd","adbc","badc","dacb","cadb","cbda","abad"]
operations[["encrypt","abcd"],["decrypt","eizfeiam"]]
expected["eizfeiam", "2"]
Checking account…