We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
staticstringsortString(strings){constintMAX_CHAR=26;int[]charCount=newint[MAX_CHAR];StringBuildersb=newStringBuilder();foreach(charchins){charCount[ch-'a']++;}for(inti=0;i<MAX_CHAR;i++){for(intj=0;j<charCount[i];j++){sb.Append('a'+i);}}returnsb.ToString();}staticintcalculateTotalCombinations(intn){return(n*(n-1))/2;}staticDictionary<string,int>findSubstrings(strings){Dictionary<string,int>dic=newDictionary<string,int>();for(inti=1;i<=s.Length;i++){for(intj=0;j<=s.Length-i;j++){varsubstring=sortString(s.Substring(j,i));// Using Substring() function if(dic.ContainsKey(substring)){dic[substring]=dic[substring]+1;continue;}dic.Add(substring,1);}}returndic;}publicstaticintsherlockAndAnagrams(strings){vardic=findSubstrings(s);inttotalAnagrams=0;foreach(varitemindic){totalAnagrams+=calculateTotalCombinations(item.Value);}returntotalAnagrams;}
Cookie support is required to access HackerRank
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 →