-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisomorphicString.ts
More file actions
36 lines (28 loc) · 840 Bytes
/
Copy pathisomorphicString.ts
File metadata and controls
36 lines (28 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function isIsomorphic(s: string, t: string): boolean {
const mapS = new Map<string, number>();
const mapT = new Map<string, number>();
const length = s.length;
for (let index = 0; index < length; index++) {
if (!mapS.has(s.charAt(index)) && mapT.has(t.charAt(index))) {
return false;
}
if (mapS.has(s.charAt(index)) && !mapT.has(t.charAt(index))) {
return false;
}
if (mapS.has(s.charAt(index)) && mapT.has(t.charAt(index))) {
if (mapS.get(s.charAt(index)) !== mapT.get(t.charAt(index))) {
return false;
}
mapS.set(s.charAt(index), index);
mapT.set(t.charAt(index), index);
}
if (!mapS.has(s.charAt(index))) {
mapS.set(s.charAt(index), index);
}
if (!mapT.has(t.charAt(index))) {
mapT.set(t.charAt(index), index);
}
}
return true;
}
console.log(isIsomorphic('paper', 'title'));