C# solution using dictionary O(n)


  • 0
    Y
    public class Solution {
            public bool IsIsomorphic(string s, string t) {
                Dictionary<char,char> isoList=new Dictionary<char,char>();
                for(int i=0;i<s.Length;i++)
                {
                    if(isoList.ContainsKey(s[i]))
                    {
                        if(isoList[s[i]]!=t[i])
                            return false;
                    }
                    else
                    {
                        if(isoList.ContainsValue(t[i]))
                            return false;
                        isoList.Add(s[i],t[i]);
                    }
                }
                return true;
            }
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.