The test suits is incomplete


  • 0
    Y

    Given s = "baa" and t = "cfa", my code returns true and is accepted even though s and t are clearly not isomorphic:

    class Solution:
        # @param {string} s
        # @param {string} t
        # @return {boolean}
        def isIsomorphic(self, s, t):
            if len(s) != len(t):
                return False
            if s == t:
                return True
    
            # char -> idx
            ds = {}
            dt = {}
            for i in range(len(s)):
                a = s[i]
                b = t[i]
    
                if a == b:
                    if a in ds and b in dt and ds[a] != dt[b]:
                        return False 
                    ds[a] = dt[b] = i
                    continue
    
                idx_s = ds[a] if a in ds else -1
                idx_t = dt[b] if b in dt else -1
    
                if idx_s < 0 and idx_t < 0:
                    ds[a] = dt[b] = i
                elif idx_s != idx_t:
                    return False
            return True
    

    The correct solution:

    class Solution:
        # @param {string} s
        # @param {string} t
        # @return {boolean}
        def isIsomorphic(self, s, t):
            if len(s) != len(t):
                return False
            if s == t:
                return True
    
            # char -> idx
            ds = {}
            dt = {}
            for i in range(len(s)):
                a = s[i]
                b = t[i]
    
                idx_s = ds[a] if a in ds else -1
                idx_t = dt[b] if b in dt else -1
    
                if idx_s < 0 and idx_t < 0:
                    ds[a] = dt[b] = i
                elif idx_s != idx_t:
                    return False
            return True

  • 0

    Thanks for your test case, I have just added this new test case.


Log in to reply
 

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