Skip to content

[205] Isomorphic Strings

https://leetcode.com/problems/isomorphic-strings/description/

  • algorithms
  • Easy (35.99%)
  • Source Code: 205.isomorphic-strings.py
  • Total Accepted: 195.9K
  • Total Submissions: 528.9K
  • Testcase Example: '"egg"\n"add"'

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

Two strings 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.

Example 1:

Input: s = "egg", t = "add" Output: true

Example 2:

Input: s = "foo", t = "bar" Output: false

Example 3:

Input: s = "paper", t = "title" Output: true

Note: You may assume both s and t have the same length.

python
class Solution(object):
    def isIsomorphic(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if not len(s) == len(t): return False
        table = {}
        for idx, c in enumerate(s):
            if c not in table:
                table[c] = t[idx]
            else:
                if not table[c] == t[idx]: return False

        values = table.values()
        return True if len(set(values)) == len(values) else False

Last updated: