FastPrepEncrypt Letters by First-Occurrence Substitution

Encrypt Letters by First-Occurrence Substitution

Palantir logoPalantir● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

For this exercise, encrypt the letters of text with a first-occurrence substitution. The first distinct letter encountered maps to a, the second to b, and so on. Uppercase and lowercase versions of a letter share one mapping identity.

Encrypt every letter occurrence with its assigned alphabet rank and preserve that occurrence's case. For example, a letter assigned rank zero becomes a when lowercase and A when uppercase.

For this exercise, assume nonletters are omitted from the ciphertext and do not consume a mapping rank. Return an empty string when the input contains no letters.

Function

encryptSubstitution(text: String) → String

Examples

Example 1

text = "bkmw"return = "abcd"

The reported illustrative letter order b, k, m, w becomes a, b, c, d under the declared encryption direction.

Example 2

text = "B!bK-k4M"return = "AaBbC"

The same letter identity has one rank in either case; punctuation and digits are omitted.

Constraints

  • For this exercise, assume 0 <= text.length <= 500.
  • Input consists of printable ASCII characters with codes 32 through 126. Only A through Z and a through z are letters.

More Palantir problems

See Palantir hiring insights
public String encryptSubstitution(String text) {
    // Write your code here.
    return "";
}
text"bkmw"
expected"abcd"
Checking account…