You are viewing a single comment's thread. Return to all comments →
c# implementation of your code:
static int sherlockAndAnagrams(string s) { var dic = new Dictionary<string, int>(); var count = 0; foreach (var substring in getSubstring(s)) { Console.WriteLine(substring); if (dic.ContainsKey(substring)) { var value = dic[substring]; count += value; dic[substring] = value + 1; } else { dic.Add(substring, 1); } } return count; } static IEnumerable<string> getSubstring(string s) { for (var i=0; i<s.Length; i++) { for (var j=1; j<=s.Length - i; j++) { var substring = s.Substring(i, j); var chars = substring.ToCharArray(); Array.Sort(chars); yield return new string(chars); } } }
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# implementation of your code: