leetcode-study

205. Isomorphic Strings

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        """
        Determine if two strings are isomorphic, meaning there is a one-to-one mapping 
        between every character in s to a character in t.
        
        Parameters:
            s (str): The first string to be compared.
            t (str): The second string to be compared.
            
        Returns:
            bool: True if the strings are isomorphic, False otherwise.
        """
        # Dictionary to map characters from s to t.
        char_map = {}
        # Set to track characters from t that have already been mapped.
        mapped_chars = set()
        
        # Iterate over paired characters from s and t.
        for char_s, char_t in zip(s, t):
            # If we already have a mapping for the current character in s.
            if char_s in char_map:
                # Check if the previously mapped character matches the current character from t.
                if char_map[char_s] != char_t:
                    # The mapping does not match, so the strings are not isomorphic.
                    return False
            else:
                # If the current character from t is already mapped to another character from s,
                # then we cannot map the current char_s to char_t.
                if char_t in mapped_chars:
                    return False
                # Establish a new mapping from char_s to char_t.
                char_map[char_s] = char_t
                # Mark char_t as used in the mapping.
                mapped_chars.add(char_t)
        
        # All character mappings are consistent; the strings are isomorphic.
        return True

Summary of Techniques and Approaches: