Loading
Leetcode / isomorphic-strings

Pick a programming language:

Here is the source code for the solution to this problem.

public class Solution {
    public bool IsIsomorphic(string s, string t) {
        Dictionary<Char, int> dict1 = new Dictionary<Char, int>();
        Dictionary<Char, int> dict2 = new Dictionary<Char, int>();
        int next1 = 0;
        int next2 = 0;

        for (int i = 0; i < s.Length; i++)
        {
            if (!dict1.ContainsKey(s[i]))
            {
                dict1[s[i]] = next1;
                next1++;
            }
            if (!dict2.ContainsKey(t[i]))
            {
                dict2[t[i]] = next2;
                next2++;
            }

            if (dict1[s[i]] != dict2[t[i]])
            {
                return false;
            }
        }

        return true;
    }
}
Did you like the lesson? 😆👍
Consider a donation to support our work: