From 9483a652ee511371cbfe405425417a7c9f1d2583 Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Sun, 5 Dec 2021 16:43:47 +0800 Subject: [PATCH] =?UTF-8?q?Update=200205.=E5=90=8C=E6=9E=84=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0205.同构字符串.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md index a2963149..eeda7d2e 100644 --- a/problems/0205.同构字符串.md +++ b/problems/0205.同构字符串.md @@ -90,6 +90,24 @@ class Solution { ## Python ```python +class Solution: + def isIsomorphic(self, s: str, t: str) -> bool: + default_dict1 = defaultdict(str) + default_dict2 = defaultdict(str) + + if len(s) != len(t): return false + + for i in range(len(s)): + if not default_dict1[s[i]]: + default_dict1[s[i]] = t[i] + + if not default_dict2[t[i]]: + default_dict2[t[i]] = s[i] + + if default_dict1[s[i]] != t[i] or default_dict2[t[i]] != s[i]: + return False + + return True ``` ## Go