Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,954 questions

51,896 answers

573 users

How to check if two strings are isomorphic in Go

2 Answers

0 votes
package main

import (
    "fmt"
)

func areIsomorphic(s, t string) bool {
    if len(s) != len(t) {
        return false
    }

    mapST := make(map[rune]rune)
    mapTS := make(map[rune]rune)

    for i, c1 := range s {
        c2 := rune(t[i])

        // Check mapping from s → t
        if mapped, ok := mapST[c1]; ok {
            if mapped != c2 {
                return false
            }
        } else {
            mapST[c1] = c2
        }

        // Check mapping from t → s
        if mapped, ok := mapTS[c2]; ok {
            if mapped != c1 {
                return false
            }
        } else {
            mapTS[c2] = c1
        }
    }
    
    return true
}

func main() {
    fmt.Println(areIsomorphic("egg", "add"))     
    fmt.Println(areIsomorphic("foo", "bar"))     
    fmt.Println(areIsomorphic("paper", "title")) 
}



/*
run:

true
false
true

*/

 



answered Dec 19, 2025 by avibootz
0 votes
package main

import (
    "fmt"
)

func areIsomorphic(s, t string) bool {
    if len(s) != len(t) {
        return false
    }

    var mapST [256]int
    var mapTS [256]int

    for i := 0; i < len(s); i++ {
        c1 := s[i]
        c2 := t[i]

        if mapST[c1] != mapTS[c2] {
            return false
        }

        mapST[c1] = i + 1
        mapTS[c2] = i + 1
    }
    
    return true
}

func main() {
    fmt.Println(areIsomorphic("egg", "add"))     
    fmt.Println(areIsomorphic("foo", "bar"))     
    fmt.Println(areIsomorphic("paper", "title")) 
}



/*
run:

true
false
true

*/

 



answered Dec 19, 2025 by avibootz
...