[리트코드] 205. Isomorphic Strings

박형진·2022년 12월 15일
0

https://leetcode.com/problems/isomorphic-strings/?envType=study-plan&id=level-1


class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        d = defaultdict(str)
        alpha = defaultdict(int)

        for i in range(len(s)):
            if s[i] not in d:
                d[s[i]] = t[i]
                alpha[t[i]] += 1
                if alpha[t[i]] >= 2:
                    return False
            else:
                if d[s[i]] != t[i]:
                    return False
        return True

문자열 s와t를 각각 KEYVALUE로 취급하여 해시 개념으로 문제를 풀었다.

  • 서로 다른 알파벳이 같은 알파벳을 매핑할 경우 False (a, b -> c)
  • 어떤 하나의 알파벳이 이미 매핑된 알파벳 이외의 또 다른 알파벳과 매핑되는 경우 False (a -> b, c)
profile
안녕하세요!

0개의 댓글