Canonicalize Usernames Under Character Equivalence Rules
Last updated: November 29, 2026
Quick Overview
Given two equal-length strings s1 and s2 where s1[i] and s2[i] are declared equivalent characters (equivalence is reflexive, symmetric, and transitive), convert a username into its canonical form by replacing each character with the smallest character equivalent to it.
Meta
November 29, 20260
0
0 solved
Given two equal-length strings s1 and s2 where s1[i] and s2[i] are declared equivalent characters (equivalence is reflexive, symmetric, and transitive), convert a username into its canonical form by replacing each character with the smallest character equivalent to it.
A Meta technical screen question with an integrity angle: detecting lookalike Instagram handles by normalizing characters that impersonators treat as interchangeable. The interviewer checks that you see union find over 26 letters and the trick of always making the smaller letter the root.
What the Interviewer Expects
- Model equivalences as unions over 26 letters
- Keep the lexicographically smallest letter as each root
- Map each username character through find
- Note the alphabet size makes everything effectively constant
- Pass through characters outside the alphabet unchanged
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you extend this to Unicode confusables like Cyrillic letters?
- How would you detect two handles that canonicalize to the same string at scale?
- What if equivalences can map one character to two (like 'rn' and 'm')?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
s1,s2of equal length (lowercase), andname. s1[i] ~ s2[i]for eachi, closed under transitivity.- Output:
namewith each letter replaced by the smallest letter in its class....
High-Level Design
Equivalence classes over a tiny alphabet are a perfect union find use. The twist is choosing the root: when merging two classes, make the smaller letter the parent. Then find(c) directly returns the...