C#, the solution beats 100% of C# submissions


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

  • 0
    W

    dictionary.ContainsValue is an O(n) function. If you sacrifice more space for another hashset, you can cut down on that call.


Log in to reply
 

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