From 4c14d9864484a0c235d6c5f76b1aedda4d1827a8 Mon Sep 17 00:00:00 2001 From: Vasily Guzov Date: Fri, 22 Nov 2024 05:07:08 +0300 Subject: [PATCH] [HAI] isIsomorphic my solution --- lib/isIsomorphic/index.ts | 3 ++ lib/isIsomorphic/isIsomorphic.test.ts | 8 +++ lib/isIsomorphic/isIsomorphic.ts | 16 ++++++ lib/isIsomorphic/readme.md | 77 +++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 lib/isIsomorphic/index.ts create mode 100644 lib/isIsomorphic/isIsomorphic.test.ts create mode 100644 lib/isIsomorphic/isIsomorphic.ts create mode 100644 lib/isIsomorphic/readme.md diff --git a/lib/isIsomorphic/index.ts b/lib/isIsomorphic/index.ts new file mode 100644 index 0000000..06a49ac --- /dev/null +++ b/lib/isIsomorphic/index.ts @@ -0,0 +1,3 @@ +import { isIsomorphic } from "./isIsomorphic"; + +export default isIsomorphic; diff --git a/lib/isIsomorphic/isIsomorphic.test.ts b/lib/isIsomorphic/isIsomorphic.test.ts new file mode 100644 index 0000000..499958d --- /dev/null +++ b/lib/isIsomorphic/isIsomorphic.test.ts @@ -0,0 +1,8 @@ +import { describe, it, expect } from "vitest"; +import { isIsomorphic } from "./isIsomorphic.ts"; + +describe("isIsomorphic", () => { + it("badc babb => false", () => { + expect(isIsomorphic("badc", "babb")).toBeFalsy(); + }); +}); diff --git a/lib/isIsomorphic/isIsomorphic.ts b/lib/isIsomorphic/isIsomorphic.ts new file mode 100644 index 0000000..f30ee1a --- /dev/null +++ b/lib/isIsomorphic/isIsomorphic.ts @@ -0,0 +1,16 @@ +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; +} diff --git a/lib/isIsomorphic/readme.md b/lib/isIsomorphic/readme.md new file mode 100644 index 0000000..6a6f8f3 --- /dev/null +++ b/lib/isIsomorphic/readme.md @@ -0,0 +1,77 @@ +## explenation + +https://leetcode.com/problems/isomorphic-strings/submissions/1459140688/ + +13:21 + +s = paper +t = title + +```ts +{ + p: t, + a: i, + p: t, + e: l, + r: e +} +``` + +мы заводим хэш таблицу в которой клчами будут симвлоы из строки s +a значениями будут буквы из слова t +если при ключ уже существуе в таблице мы проверим какое в нем значение +если оно совпадает с текущим и слова t мы продолжим итерацию +если не совпадает мы вернем фалс +перед началом мы сразу сравним длинну строк и выкиним фалсе если она разная + +оценка + +time: O(n) +mem: 0(n) + +## first implementation + +09:00 + +## ошибочный ответ + +### ошибка проверки ключей + +36:00 + +проверял только наличие ключей и значение в них, но не учел +кейс когда значение из t уже есть в хэш таблице под другим ключом + +нужно завести вторую таблицу и наполнять ее инвертированно ключи из t значения из s +и сравнивать их + +s = "badc" +t = "baba" + +```ts +ht1 = { + b: b, + a: a, + d: b, + c: a, +}; + +b: b, +b: b, +a: a, +a: a, +d: b, +b: d, + + +ht2 = { + b: b, + a: a, + b: d, + a: c, +}; +``` + +## общее время + +01:11:25