How to check whether the given strings are isomorphic using C#?

Two strings, X and Y, are called isomorphic if all occurrences of each character in X can be replaced with another character to get Y and vice-versa. For example, consider strings ACAB and XCXY. 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.

Example 1

Input − s = "egg", t = "add"

Output − true

Example 2

Input − s = "foo", t = "bar"

Output − false

Time complexity − O(N)

Space complexity − O(N)


public class Arrays{
   public bool IsStringIsomorphic(string s, string t){
      if (s == null || t == null){
         return false;
      int[] chars1 = new int[128];
      int[] chars2 = new int[128];
      for (int i = 0; i < s.Length; i++){
         if (chars1[s[i]] != chars2[t[i]]){
            return false;
            chars1[s[i]] = i + 1;
            chars2[t[i]] = i + 1;
      return true;

static void Main(string[] args){
   Console.WriteLine(s.IsStringIsomorphic("add", "egg"));