[HAI] isIsomorphic etalon solution
parent
4c14d98644
commit
baabc7acb8
@ -1,16 +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 {
|
export function isIsomorphic(s: string, t: string): boolean {
|
||||||
let ht: Record<string, string> = {};
|
let sMap = {};
|
||||||
let ht2: Record<string, string> = {};
|
let tMap = {};
|
||||||
if (s.length !== t.length) return false;
|
|
||||||
|
|
||||||
for (let [index, key] of Object.entries(s)) {
|
for (let i = 0; i < s.length; i++) {
|
||||||
const currentValue = t[index];
|
if (sMap[s[i]] && sMap[s[i]] !== t[i]) {
|
||||||
if (key in ht && currentValue !== ht[key]) return false;
|
return false;
|
||||||
if (currentValue in ht2 && key !== ht2[currentValue]) return false;
|
}
|
||||||
|
|
||||||
ht[key] = currentValue;
|
if (tMap[t[i]] && tMap[t[i]] !== s[i]) {
|
||||||
ht2[currentValue] = key;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sMap[s[i]] = t[i];
|
||||||
|
tMap[t[i]] = s[i];
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue