Grokking LinkedIn Coding Interview
Ask Author
Back to course home

0% completed

Solution: Isomorphic Strings

Problem Statement

Given two strings s and t, return true if they are isomorphic. Otherwise, return false.

Two strings s and t are called isomorphic if the characters in string s can be replaced to get string t.

Each character in string t should be replaced with another character while preserving the order of characters as in string s. Two characters should not map to the same character, but a character may map to itself.

Examples

  • Example 1:

    • Input: s = "abb", t = "cdd"
    • Expected Output: true
    • Justification: Each character in "abb" can be replaced to match the corresponding character in "cdd" (a->c, b->d) while maintaining a one-to-one mapping.
  • Example 2:

    • Input: s = "cbcrt", t = "abaxv"
    • Expected Output: true
    • Justification: The characters in "cbcrt" can be replaced to form "abaxv" with a unique mapping for each character (c->a, b->b, r->x, t->v).
  • Example 3:

    • Input: s = "abcd", t = "bbcd"
    • Expected Output: false
    • Justification: The first string's characters cannot be replaced to form the string t while maintaining a unique mapping, as 'b' in the second string corresponds to both 'a' and 'b' in the s.

Solution

To solve this problem, we'll use a mapping strategy to track the transformation of characters from the first string to the second. This approach works by ensuring that each character in the first string can uniquely map to a character in the second string, and vice versa. The reason this method is effective lies in its ability to maintain a one-to-one correspondence between the characters of both strings, which is crucial for the strings to be isomorphic. By using two hash maps (or dictionaries) to store these mappings in both directions, we can efficiently check the conditions for isomorphism. This dual-mapping technique allows us to verify that no two characters in the first string map to the same character in the second string and that the mapping is consistent throughout both strings.

Step-by-step Algorithm

  1. Initialize Two Hash Maps: Start by creating two empty hash maps (or dictionaries). The first map (mapS2T) will store mappings from characters in string s to string t. The second map (mapT2S) will store mappings in the opposite direction, from characters in string t to string s.

  2. Iterate Over Characters: Loop through each character of the strings s and t simultaneously. For each position i, get the characters charS from s and charT from t.

  3. Check and Update Mappings:

    • For each character pair (charS, charT), perform the following checks:
      • If charS is already mapped in mapS2T, check if it maps to the current charT. If not, the strings are not isomorphic, return false.
      • Similarly, if charT is already mapped in mapT2S, check if it maps to the current charS. If not, return false.
    • If both checks pass, update mapS2T by mapping charS to charT and mapT2S by mapping charT to charS.
  4. Return True: If the loop completes without finding any inconsistencies in the mappings, return true. This means that the strings are isomorphic.

Algorithm Walkthrough

Let's consider the input: s = "cbcrt", t = "abaxv"

  • Initial State: mapS2T = {}, mapT2S = {}

  • Step 1: (c, a)

    • mapS2T does not contain c, and mapT2S does not contain a.
    • Update maps: mapS2T = {c: a}, mapT2S = {a: c}
  • Step 2: (b, b)

    • mapS2T does not contain b, and mapT2S does not contain b.
    • Update maps: mapS2T = {c: a, b: b}, mapT2S = {a: c, b: b}
  • Step 3: (c, a)

    • mapS2T contains c mapping to a, and mapT2S contains a mapping to c. The mappings are consistent.
    • No update needed. Maps remain: mapS2T = {c: a, b: b}, mapT2S = {a: c, b: b}
  • Step 4: (r, x)

    • mapS2T does not contain r, and mapT2S does not contain x.
    • Update maps: mapS2T = {c: a, b: b, r: x}, mapT2S = {a: c, b: b, x: r}
  • Step 5: (t, v)

    • mapS2T does not contain t, and mapT2S does not contain v.
    • Update maps: mapS2T = {c: a, b: b, r: x, t: v}, mapT2S = {a: c, b: b, x: r, v: t}
  • Final State: After iterating through all character pairs, the mappings in both mapS2T and mapT2S are consistent and complete without any conflicts. Thus, return true.

Image

Code

Python3
Python3

. . . .

Complexity Analysis

Time Complexity: O(N) - where N is the length of the strings. The algorithm iterates over each character of the strings exactly once to check or create a mapping.

Space Complexity: O(1) - The space used by the hash maps does not depend on the size of the input strings directly since the number of unique characters that can be stored is limited by the character set (e.g., ASCII has 128 unique characters, extended ASCII has 256). Therefore, the space complexity can be considered as O(1) in terms of the input size.

Mark as Completed