Algo.Isomorphing strings

isomorphing strings on leetcode

    public bool IsIsomorphic(string s, string t) {
            if (s.Length != t.Length)
                return false;

            var firstStringDict = new Dictionary<char, char>();
            var secondStringDict = new Dictionary<char, char>();

            for (var i = 0; i < s.Length; i++)
            {
                // egg add -> true
                // foo bar -> false
                if (firstStringDict.Keys.Contains(s[i]))
                {
                    firstStringDict.TryGetValue(s[i], out var value);
                    if (value != t[i])
                        return false;
                }
                
                if (secondStringDict.Keys.Contains(t[i]))
                {
                    secondStringDict.TryGetValue(t[i], out var value);
                    if (value != s[i])
                        return false;
                }                   

                if (!firstStringDict.Keys.Contains(s[i]))
                    firstStringDict.Add(s[i], t[i]);                
                
                if (!secondStringDict.Keys.Contains(t[i]))
                    secondStringDict.Add(t[i], s[i]);
            }

            return true;        
    }
This entry was posted in Без рубрики. Bookmark the permalink.