FrontendInterviews.dev

Loading problem…

70. Isomorphic Strings - Pattern Matching

Easy•
Acceptance: 90.91%
•
🔓3/3 Pro unlocks today

You're building a form validation system that needs to check if two strings follow the same character mapping pattern. This is useful for validating password patterns, detecting similar structures, or matching templates.

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

Requirements

1. Basic Functionality

  • Check if two strings have isomorphic character mapping
  • Each character in s maps to exactly one character in t
  • Each character in t maps to exactly one character in s
  • Preserve character order
  • Handle empty strings (return true)

Example Usage

isIsomorphic("egg", "add");     // true - e→a, g→d
isIsomorphic("foo", "bar");     // false - o→a and o→r (conflict)
isIsomorphic("paper", "title"); // true - p→t, a→i, e→l, r→e

Real-World Context

This problem models real validation scenarios:

  • Password pattern matching: Check if two passwords follow same pattern
  • Template validation: Verify if strings match template structure
  • String similarity: Detect if strings have isomorphic structure
  • Form validation: Validate input patterns

Constraints

  • 1 <= s.length <= 5 * 10^4
  • t.length == s.length
  • s and t consist of any valid ascii characters
  • Mapping must be bijective (one-to-one and onto)