Skip to content

Latest commit

 

History

History
39 lines (32 loc) · 1.1 KB

File metadata and controls

39 lines (32 loc) · 1.1 KB

Solution 3: Morphic Mapping (Isomorphic Strings)

Approach Explanation

We maintain two hash maps for bi-directional mapping between characters of s and t. Alternatively, we can use a single map and a set to ensure no two characters map to the same target.

Step-by-Step Logic

  1. If len(s) != len(t), return False.
  2. Initialize mapping_s_t = {}, mapped_t_chars = set().
  3. For each pair of characters (c1, c2) in zip(s, t):
    • If c1 in mapping_s_t:
      • If mapping_s_t[c1] != c2, return False.
    • Else:
      • If c2 in mapped_t_chars, return False.
      • mapping_s_t[c1] = c2, mapped_t_chars.add(c2).
  4. Return True.

Complexity

  • Time Complexity: O(N).
  • Space Complexity: O(1) (size of character set).

Code

def is_isomorphic(s, t):
    if len(s) != len(t): return False
    
    map_s = {}
    map_t = {}
    
    for c1, c2 in zip(s, t):
        if c1 in map_s:
            if map_s[c1] != c2: return False
        elif c2 in map_t:
            return False
        else:
            map_s[c1] = c2
            map_t[c2] = c1
            
    return True