From baabc7acb8239b958bb1f45c2897104775ecb2f9 Mon Sep 17 00:00:00 2001 From: Vasily Guzov Date: Sat, 23 Nov 2024 04:36:49 +0300 Subject: [PATCH] [HAI] isIsomorphic etalon solution --- lib/isIsomorphic/isIsomorphic.ts | 40 ++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/isIsomorphic/isIsomorphic.ts b/lib/isIsomorphic/isIsomorphic.ts index f30ee1a..5de7e8b 100644 --- a/lib/isIsomorphic/isIsomorphic.ts +++ b/lib/isIsomorphic/isIsomorphic.ts @@ -1,16 +1,36 @@ +// export function isIsomorphic(s: string, t: string): boolean { +// let ht: Record = {}; +// let ht2: Record = {}; +// if (s.length !== t.length) return false; + +// for (let [index, key] of Object.entries(s)) { +// const currentValue = t[index]; +// if (key in ht && currentValue !== ht[key]) return false; +// if (currentValue in ht2 && key !== ht2[currentValue]) return false; + +// ht[key] = currentValue; +// ht2[currentValue] = key; +// } + +// return true; +// } + +// etalon export function isIsomorphic(s: string, t: string): boolean { - let ht: Record = {}; - let ht2: Record = {}; - if (s.length !== t.length) return false; + let sMap = {}; + let tMap = {}; - for (let [index, key] of Object.entries(s)) { - const currentValue = t[index]; - if (key in ht && currentValue !== ht[key]) return false; - if (currentValue in ht2 && key !== ht2[currentValue]) return false; + for (let i = 0; i < s.length; i++) { + if (sMap[s[i]] && sMap[s[i]] !== t[i]) { + return false; + } - ht[key] = currentValue; - ht2[currentValue] = key; - } + if (tMap[t[i]] && tMap[t[i]] !== s[i]) { + return false; + } + sMap[s[i]] = t[i]; + tMap[t[i]] = s[i]; + } return true; }