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 Kotlin

2 Answers

0 votes
fun areIsomorphic(s: String, t: String): Boolean {
    if (s.length != t.length) return false

    val mapST = mutableMapOf<Char, Char>()
    val mapTS = mutableMapOf<Char, Char>()

    for ((c1, c2) in s.zip(t)) {
        // Check mapping from s → t
        if (mapST.containsKey(c1)) {
            if (mapST[c1] != c2) return false
        } else {
            mapST[c1] = c2
        }

        // Check mapping from t → s
        if (mapTS.containsKey(c2)) {
            if (mapTS[c2] != c1) return false
        } else {
            mapTS[c2] = c1
        }
    }
    
    return true
}

fun main() {
    println(areIsomorphic("egg", "add"))     
    println(areIsomorphic("foo", "bar"))     
    println(areIsomorphic("paper", "title")) 
}



/*
run:

true
false
true

*/

 



answered Dec 19, 2025 by avibootz
0 votes
fun areIsomorphic(s: String, t: String): Boolean {
    fun pattern(str: String) = str.map { str.indexOf(it) }
    
    return pattern(s) == pattern(t)
}

fun main() {
    println(areIsomorphic("egg", "add"))     
    println(areIsomorphic("foo", "bar"))     
    println(areIsomorphic("paper", "title")) 
}



/*
run:

true
false
true

*/

 



answered Dec 19, 2025 by avibootz
...