1 line Python Solution, 95%


  • 5
    A

    Say we have 2 strings 'add' and 'egg':
    for the result to be true, one letter in the first string must have an unique mapping to one letter in the other string.
    'a'->'e' and 'd'->'g'
    And the number of such mapping should be the SAME as the number of different letters in the 2 strings.
    And that is all we need to check.

    class Solution(object):
        def isIsomorphic(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            return len(set(s)) == len(set(zip(s, t))) == len(set(t))
    

  • 1
    R

    it's very smart!


  • 0
    R

    This is a best solution I have seen so far. Explanation of your logic would help.


  • 0
    A

    @rahul8590
    Explanation added. Hope it would help!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.