From 93f29ed6d5cce50ccaf057a8a3177318c105b344 Mon Sep 17 00:00:00 2001 From: jerryfishcode <91447694+jerryfishcode@users.noreply.github.com> Date: Mon, 27 Sep 2021 20:55:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20205.=20=E5=90=8C=E6=9E=84?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=20JavaScript=E8=A7=A3=E6=B3=95?= 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 5d20aa4a..1b4d0e32 100644 --- a/problems/0205.同构字符串.md +++ b/problems/0205.同构字符串.md @@ -119,6 +119,25 @@ func isIsomorphic(s string, t string) bool { ## JavaScript ```js +var isIsomorphic = function(s, t) { + let len = s.length; + if(len === 0) return true; + let maps = new Map(); + let mapt = new Map(); + for(let i = 0, j = 0; i < len; i++, j++){ + if(!maps.has(s[i])){ + maps.set(s[i],t[j]);// maps保存 s[i] 到 t[j]的映射 + } + if(!mapt.has(t[i])){ + mapt.set(t[j],s[i]);// mapt保存 t[j] 到 s[i]的映射 + } + // 无法映射,返回 false + if(maps.get(s[i]) !== t[j] || mapt.get(t[j]) !== s[i]){ + return false; + } + }; + return true; +}; ``` -----------------------