VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-two-given-strings-are-isomorphic-to-each-other/

⇱ Isomorphic Strings Check - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Isomorphic Strings Check

Last Updated : 2 Jul, 2025

Given two strings s1 and s2 of equal length, consisting only of lowercase English letters, determine if they are isomorphic.
Two strings are isomorphic if characters in s1 can be replaced to get s2 such that:

  • Each character in s1 maps to a unique character in s2.
  • The mapping is consistent throughout the string.
  • The order of characters is preserved.

Examples: 

Input: s1 = "aab", s2 = "xxy"
Output: true
Explanation: Each character in s1 can be consistently mapped to a unique character in s2 (a → x, b → y).

Input: s1 = "aab", s2 = "xyz"
Output: false
Explanation: Same character 'a' in s1 maps to two different characters 'x' and 'y' in s2.

Input: s1 = "abc", s2 = "xxz"
Output: false
Explanation: Two different characters 'a' and 'b' in s1 maps with same character 'x' in s2.

[Naive Approach] Check Every Character - O(n^2) Time and O(1) Space

A simple Solution is to consider every character of 's1' and check if all occurrences of it map to the same character in 's2'. 


Output
true

[Expected Approach 1] Using Hash Maps

The idea is based on the fact that all occurrences of two characters should be at same index. We mainly store the first index of every character and for remaining occurrences, we check if they appear at same first index too.

We mainly use two maps m1 and m2 to store characters as keys and their first indexes as values.

  • If this character is seen first time in s1, then store is index in map m1.
  • If this character is seen first time in s2, then store is index in map m2.
  • If indexes in map for both the characters do not match, return false.

Output
true

Time Complexity: O(n) We traverse both strings once, and all hash map operations (insert and lookup) take constant time on average.
Auxiliary Space: O(1)  Since the strings contain only lowercase English letters, the maximum size of each hash map is bounded by 26, making it constant space.

[Expected Approach 2] Fixed-Size Array Mapping Approach - O(n) Time and O(1) Space

We avoid language-specific hash overhead by using fixed-size arrays, leveraging the 26-letter lowercase alphabet. Each character is mapped to an index ('a' → 0, ..., 'z' → 25) for constant-time access. One array stores the character mappings, while another tracks if a character in the second string is already used — ensuring a one-to-one correspondence.


Output
true

[Expected Approach 3] Using HashMap and Set - O(n) Time and O(1) Space

The idea is to store mapping of characters from s1 to s2 in a map and already seen characters of s2 in a set.

Step by Step Implementations:

  • Initialize a mapping structure (e.g., dictionary or hashmap) to store character mappings from s1 to s2.
  • Initialize a set to keep track of characters from s2 that have already been mapped.
  • For each position i in the strings:
  • If s1[i] is already in the mapping, check if it maps to s2[i]; if not, return false.
  • If s1[i] is not in the mapping and s2[i] is already in the set, return false.
  • Add a mapping from s1[i] to s2[i].
  • Add s2[i] to the set of used characters.
  • If all characters are processed without conflicts, return true.

Output
true
Comment
Article Tags:
Article Tags: