-
Notifications
You must be signed in to change notification settings - Fork 0
/
205.go
39 lines (32 loc) · 917 Bytes
/
205.go
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
package p205
/**
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.
*/
func isIsomorphic(s string, t string) bool {
if len(s) != len(t) {
return false
}
hms2t := make(map[byte]byte)
hmt2s := make(map[byte]byte)
for i := 0; i < len(s); i++ {
vs2t, oks2s := hms2t[s[i]]
vt2s, okt2s := hmt2s[t[i]]
if !oks2s && !okt2s {
hms2t[s[i]] = t[i]
hmt2s[t[i]] = s[i]
} else if okt2s && oks2s {
if vs2t != t[i] || vt2s != s[i] {
return false
}
} else {
return false
}
}
return true
}