Leet
InterviewTime limit2sMemory limit128 MB
Decide whether the plain word can be split so each distinct letter maps to one fixed leet block of length at most k.
- Level
Medium6 of 10
- Topics
- Backtracking, String, Hash map
- Solved
- No attempts yet
Problem
Leet is a way of writing in which each letter of the alphabet is replaced by one or more other characters, used mostly online. For example, replacing A with 4, E with 3, and T with 7 turns the word "ATE" into "437". A single letter may also be replaced by a string longer than one character, such as writing M as |V|.
A letter can sometimes be written in leet in more than one way (for example, D could be [), |), or |>), which is what makes leet hard to read.
Given an original word written in lowercase letters and a word written in leet, write a program that decides whether the two words correspond to each other under all of the following rules.
- When a letter is converted to leet, its replacement string has length at most (given in the input); that is, the length is between 1 and .
- Each letter maps to exactly one leet string. So if the same letter appears several times in the original word, every occurrence must be replaced by the same string. For example, if D is set to
[), then D cannot also be|>in the same check. - Two different letters may map to the same leet string. For example, D and P may both become
|>. - A letter and its leet string do not need to look alike.
Input
The first line contains the number of test cases . Each test case consists of three lines.
- The first line contains , the maximum length of the leet string for a single letter ().
- The second line contains the original word, made up only of lowercase letters a-z, with length between 1 and 15.
- The third line contains the word written in leet. Its length is at least 1, and it consists of a-z, A-Z, 0-9 and the symbols
@ \ / - = ^ | [ ] ( ) { } < >.
Output
For each test case, print 1 if the original word can be turned into the leet word under the rules, and 0 otherwise, each on its own line.