205.isomorphic-strings.go 298 B

123456789101112131415
  1. func isIsomorphic(s string, t string) bool {
  2. m1 := make(map[byte]byte)
  3. m2 := make(map[byte]byte)
  4. for i := range s {
  5. b1, ok1 := m1[s[i]]
  6. b2, ok2 := m2[t[i]]
  7. if !ok1 && !ok2 {
  8. m1[s[i]] = t[i]
  9. m2[t[i]] = s[i]
  10. } else if b1 != t[i] || b2 != s[i] {
  11. return false
  12. }
  13. }
  14. return true
  15. }