-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathisIsomorphic.go
More file actions
69 lines (56 loc) · 1.38 KB
/
Copy pathisIsomorphic.go
File metadata and controls
69 lines (56 loc) · 1.38 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
62
63
64
65
66
67
68
69
/* https://leetcode.com/problems/isomorphic-strings/#/description
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.
Note:
You may assume both s and t have the same length.
*/
package lht
func isIsomorphic(s string, t string) bool {
if len(s) != len(t) {
return false
}
a, b := make([]byte, 256), make([]byte, 256)
rs, rt := []byte(s), []byte(t)
for i := range rs {
if v := a[int(rs[i])]; v == 0 {
a[int(rs[i])] = rt[i]
} else if v != rt[i] {
return false
}
if v := b[int(rt[i])]; v == 0 {
b[int(rt[i])] = rs[i]
} else if v != rs[i] {
return false
}
}
return true
}
/*
func isIsomorphic(s string, t string) bool {
m, n := len(s), len(t)
if m != n {
return false
}
mmap, nmap := make(map[byte]byte), make(map[byte]byte)
for i := range s {
if _, ok := mmap[s[i]]; !ok {
mmap[s[i]] = t[i]
}
if _, ok := nmap[t[i]]; !ok {
nmap[t[i]] = s[i]
}
if t[i] != mmap[s[i]] {
return false
}
if s[i] != nmap[t[i]] {
return false
}
}
return true
}
*/