70. Twin Encodings

Two coded messages s and t of the same length are given. They are mapped if every letter of s can be replaced by one letter so that the result is exactly t, under two rules: the same letter in s must always become the same letter in t, and two different letters in s must never become the same letter in t. A letter may be replaced by itself.

Return true if the messages are mapped and false otherwise.

Example 1

Input:
s = "moon", t = "tool"
Output:
true
Explanation:

m becomes t, o stays o, n becomes l, and no two letters share a target, so the messages are mapped.

Example 2

Input:
s = "cat", t = "dud"
Output:
false
Explanation:

Both c and t would have to become d, which breaks the rule that different letters need different targets.

Constraints

1 ≤ s.length ≤ 5 * 104

t.length == s.length

s and t contain only lowercase English letters

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…