-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path205.同构字符串.js
More file actions
39 lines (37 loc) · 810 Bytes
/
205.同构字符串.js
File metadata and controls
39 lines (37 loc) · 810 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
37
38
39
/*
* @lc app=leetcode.cn id=205 lang=javascript
*
* [205] 同构字符串
*/
// @lc code=start
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isIsomorphic = function (s, t) {
const mapS = new Map();
const mapT = new Map();
for (let i = 0; i < s.length; i++) {
if (!mapS.has(s[i])) {
mapS.set(s[i], t[i]);
}
if (!mapT.has(t[i])) {
mapT.set(t[i], s[i]);
}
}
let convertS = "";
let convertT = "";
for (let i = 0; i < s.length; i++) {
convertS += mapS.get(s[i]);
convertT += mapT.get(t[i]);
}
return convertS === t && convertT === s;
};
// 妙
// var isIsomorphic = function (s, t) {
// for (let i = 0; i < s.length; i++)
// if (s.indexOf(s[i]) !== t.indexOf(t[i])) return false;
// return true;
// };
// @lc code=end