Same Shape Words
Two words have the same shape when one can be turned into the other by renaming its letters: every copy of a letter is replaced by the same new letter, and two different letters never get the same new name. A letter may also keep its own name.
Given the words first and second, return true if they have the same shape, and false if not. Words of different lengths never have the same shape.
Examples
first = "egg", second = "add" → true
first = "foo", second = "bar" → false
first = "paper", second = "title" → true
first = "ab", second = "aa" → falseIn "paper" → "title", p becomes t, a becomes i, e becomes l and r becomes e. In "foo" → "bar" the letter o would have to become both a and r; in "ab" → "aa" two different letters would both become a.
Constraints
- Each word holds between 1 and 20,000 characters, any characters at all.
- Aim for O(n): walk the two words once, side by side.
Console
›Run to see what your code prints
Run checks the 4 visible cases here, in your browser. Submit runs all 10 on the judge, 6 of them hidden, and measures how your solution scales.
Expected is the reference solution's answer, from the judge. An input that breaks the statement's other rules may get any answer.