From a6a8650b8a6524ed03cf599f1c32fc42c78cd8e5 Mon Sep 17 00:00:00 2001 From: posper Date: Thu, 29 Jul 2021 19:32:33 +0800 Subject: [PATCH] =?UTF-8?q?205.=E5=90=8C=E6=9E=84=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=20=E6=B7=BB=E5=8A=A0Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0205.同构字符串.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md index 4e963ece..06ea5701 100644 --- a/problems/0205.同构字符串.md +++ b/problems/0205.同构字符串.md @@ -68,6 +68,25 @@ public: ## Java ```java +class Solution { + public boolean isIsomorphic(String s, String t) { + Map map1 = new HashMap<>(); + Map map2 = new HashMap<>(); + for (int i = 0, j = 0; i < s.length(); i++, j++) { + if (!map1.containsKey(s.charAt(i))) { + map1.put(s.charAt(i), t.charAt(j)); // map1保存 s[i] 到 t[j]的映射 + } + if (!map2.containsKey(t.charAt(j))) { + map2.put(t.charAt(j), s.charAt(i)); // map1保存 t[j] 到 s[i]的映射 + } + // 无法映射,返回 false + if (map1.get(s.charAt(i)) != t.charAt(j) || map2.get(t.charAt(j)) != s.charAt(i)) { + return false; + } + } + return true; + } +} ``` ## Python