Esempio n. 1
0
    public static int SherlockAndAnagrams(string s)
    {
        Dictionary <AnagramChars, int> anagrams = new Dictionary <AnagramChars, int>();

        // count dupe strings (based on byte values)
        for (int i = 0; i < s.Length; i++)
        {
            AnagramChars ac = new AnagramChars();

            for (int j = i; j < s.Length; j++)
            {
                ac.AddChar(s[j]);
                if (!anagrams.ContainsKey(ac))
                {
                    anagrams.Add(new AnagramChars(ac), 1);
                }
                else
                {
                    anagrams[ac] += 1;
                }
            }
        }
        // return sum of vertices-edge relationships for the strings we found.
        return(anagrams.Values.Sum(v => ((v * (v - 1) / 2))));
    }
Esempio n. 2
0
 public AnagramChars(AnagramChars charCount)
 {
     CharArray = new byte[26];
     for (int i = 0; i < 26; i++)
     {
         CharArray[i] = charCount.CharArray[i];
     }
 }
Esempio n. 3
0
    public override bool Equals(object obj)
    {
        AnagramChars other = obj as AnagramChars;

        if (obj == null)
        {
            return(false);
        }

        for (int i = 0; i < 26; i++)
        {
            int val = CharArray[i].CompareTo(other.CharArray[i]);
            if (val != 0)
            {
                return(false);
            }
        }

        return(true);
    }
Esempio n. 4
0
 protected bool Equals(AnagramChars other)
 {
     return(Equals(CharArray, other.CharArray));
 }