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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

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
union find
equivalence classes
string normalization
lexicographic minimum
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: s1, s2 of equal length (lowercase), and name.
  • s1[i] ~ s2[i] for each i, closed under transitivity.
  • Output: name with 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...


Submit Your Answer
Markdown supported