-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0205-isomorphic-strings.js
More file actions
61 lines (50 loc) · 1.37 KB
/
Copy path0205-isomorphic-strings.js
File metadata and controls
61 lines (50 loc) · 1.37 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//Blog: https://www.allenliservice.online/leetcode-js-205-isomorphic-strings/
// <strong>Solution:</strong>
// 1. 先設定 objectS = {}, objectT = {}。
// 2. 運用 for 迴圈次數為 s 的長度,每一輪比對內容。
// 3. 如不同則返回 false
// 4. 其餘狀況將該輪的 object內容更新。
// 5. 完整走完迴圈則返回 ture。
// <strong>Code 1:</strong>
var isIsomorphic = function (s, t) {
if (s.length !== t.length) return false;
let objectS = {},
objectT = {};
for (let i = 0; i < s.length; i++) {
if (objectS[s[i]] === objectT[t[i]]) {
objectS[s[i]] = i;
objectT[t[i]] = i;
} else {
return false;
}
}
return true;
};
/* < strong > Example 1</strong >
<pre style='background-color:#ggg'>
Input: s = "egg", t = "add"
step.1
i = 0
objectS[s[0]] = underlined, objectT[t[0]] = underlined //===
objectS[s[0]] = i //0
objectT[t[0]] = i //0
step.2
i = 1
objectS[s[1]] = underlined, objectT[t[1]] = underlined //===
objectS[s[1]] = i //1
objectT[t[1]] = i //1
step.3
i = 2
objectS[s[2]] = underlined, objectT[t[2]] = underlined //===
objectS[s[1]] = i //2
objectT[t[1]] = i //2
return true
</pre> */
// <strong>Code 2:</strong>
var isIsomorphic = function (s, t) {
if (s.length !== t.length) return false;
for (let i = 0; i < s.length; i++) {
if (s.indexOf(s[i]) !== t.indexOf(t[i])) return false;
}
return true;
};