public class Solution {
    public bool IsIsomorphic(string s, string t) {
        if (s.Length != t.Length)
            {
                return false;
            }
            else
            {
                Dictionary<char, int> dic1 = new Dictionary<char, int>();
                Dictionary<char, int> dic2 = new Dictionary<char, int>();

                int type1 = 0;
                int type2 = 0;

                StringBuilder sb1 = new StringBuilder();
                StringBuilder sb2 = new StringBuilder();

                foreach (var c in s)
                {
                    if (!dic1.ContainsKey(c))
                    {
                        dic1.Add(c, type1);
                        sb1.Append(type1);
                        type1++;
                    }
                    else
                    {
                        sb1.Append(dic1[c]);
                    }

                }

                foreach (var c in t)
                {
                    if (!dic2.ContainsKey(c))
                    {
                        dic2.Add(c, type2);
                        sb2.Append(type2);
                        type2++;
                    }
                    else
                    {
                        sb2.Append(dic2[c]);
                    }
                }

                if (sb1.ToString() != sb2.ToString())
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
    }
}

https://leetcode.com/problems/isomorphic-strings/#/description

posted on 2017-04-24 19:31  Sempron2800+  阅读(100)  评论(0编辑  收藏  举报