You are viewing a single comment's thread. Return to all comments →
C#
public static int sherlockAndAnagrams(string s) { Dictionary<string, int> map = new(); int anagramCount = 0; for(int i = 0; i < s.Length; i++) { for(int j = 1; j <= s.Length - i; j++) { char[] subChars = s.Substring(i, j).ToArray(); Array.Sort(subChars); string sub = new string(subChars); if(!map.TryAdd(sub, 1)) { int subCount = map[sub]; map[sub] += 1; anagramCount += subCount; } } } return anagramCount; }
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
C#