Isomorphic Strings - Problem

Given two strings s and t, determine if they are isomorphic.

Two strings s and t 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.

Input & Output

Example 1 — Basic Isomorphic Case
$ Input: s = "egg", t = "add"
Output: true
💡 Note: Characters map consistently: 'e'→'a', 'g'→'d'. Pattern is preserved: first char different from next two, which are identical.
Example 2 — Non-Isomorphic Case
$ Input: s = "foo", t = "bar"
Output: false
💡 Note: Character 'o' appears twice in s but maps to different characters in t: 'o'→'a' and 'o'→'r'. This violates isomorphic property.
Example 3 — Identity Mapping
$ Input: s = "paper", t = "title"
Output: true
💡 Note: Valid mappings: 'p'→'t', 'a'→'i', 'p'→'t', 'e'→'l', 'r'→'e'. Each character maps consistently to exactly one other character.

Constraints

  • 1 ≤ s.length ≤ 5 × 104
  • t.length == s.length
  • s and t consist of any valid ASCII character

Visualization

Tap to expand
Isomorphic Strings: Character Pattern Matchingeggs = "egg"addt = "add"Character Mappingse → a (consistent)g → d (consistent)Pattern: X-Y-YResult: trueSame pattern preserved: different-same-same
Understanding the Visualization
1
Input Strings
Two strings s and t of equal length
2
Character Mapping
Check if characters can be mapped consistently
3
Validation Result
Return true if mapping is bijective and consistent
Key Takeaway
🎯 Key Insight: Two strings are isomorphic if they have identical character occurrence patterns and maintain consistent one-to-one mapping
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
102.5K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen