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.

40,023 questions

51,974 answers

573 users

How to check if two strings are isomorphic in C#

2 Answers

0 votes
// Two strings are isomorphic if characters in one can be replaced to get the other, 
// while preserving the order.

using System;
using System.Collections.Generic;

class Program
{
    static bool AreIsomorphic(string s, string t) {
        if (s.Length != t.Length) return false;

        var mapST = new Dictionary<char, char>();
        var mapTS = new Dictionary<char, char>();

        for (int i = 0; i < s.Length; i++) {
            char c1 = s[i];
            char c2 = t[i];

            // 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;
    }

    static void Main()
    {
        Console.WriteLine(AreIsomorphic("egg", "add"));     
        Console.WriteLine(AreIsomorphic("foo", "bar"));     
        Console.WriteLine(AreIsomorphic("paper", "title"));
    }
}


/*
run:

True
False
True

*/

 



answered Dec 19, 2025 by avibootz
edited Dec 19, 2025 by avibootz
0 votes
using System;
using System.Collections.Generic;

class Program
{
    static bool AreIsomorphic(string s, string t) {
        if (s.Length != t.Length) return false;
    
        int[] mapST = new int[256];
        int[] mapTS = new int[256];
    
        for (int i = 0; i < s.Length; i++) {
            char c1 = s[i];
            char c2 = t[i];
    
            if (mapST[c1] != mapTS[c2]) return false;
    
            mapST[c1] = i + 1;
            mapTS[c2] = i + 1;
        }
        
        return true;
    }

    static void Main()
    {
        Console.WriteLine(AreIsomorphic("egg", "add"));     
        Console.WriteLine(AreIsomorphic("foo", "bar"));     
        Console.WriteLine(AreIsomorphic("paper", "title"));
    }
}


/*
run:

True
False
True

*/

 



answered Dec 19, 2025 by avibootz
...