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..5de7e8b --- /dev/null +++ b/lib/isIsomorphic/isIsomorphic.ts @@ -0,0 +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 sMap = {}; + let tMap = {}; + + for (let i = 0; i < s.length; i++) { + if (sMap[s[i]] && sMap[s[i]] !== t[i]) { + return false; + } + + if (tMap[t[i]] && tMap[t[i]] !== s[i]) { + return false; + } + + sMap[s[i]] = t[i]; + tMap[t[i]] = s[i]; + } + 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