Check whether two Strings are anagram of each other
An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other.
publicclassSolution{boolAreAnagrams(string str1,string str2) {if (str1.Length!=str2.Length)returnfalse;var dict =newDictionary<char,int>();foreach (char c in str1) {if (dict.ContainsKey(c))dict[c]++;elsedict[c] =1; }foreach (char c in str2) {if (!dict.ContainsKey(c))returnfalse;if (--dict[c] ==0)dict.Remove(c); }returndict.Count==0; }}
With out dictionary, but need to crate a copy from string to char array.