Merge branch 'isomorf-string'
commit
1f34e8a044
@ -0,0 +1,3 @@
|
|||||||
|
import { isIsomorphic } from "./isIsomorphic";
|
||||||
|
|
||||||
|
export default isIsomorphic;
|
||||||
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
// export function isIsomorphic(s: string, t: string): boolean {
|
||||||
|
// let ht: Record<string, string> = {};
|
||||||
|
// let ht2: Record<string, string> = {};
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue