You are viewing a single comment's thread. Return to all comments →
Inspired from etayluz clue but with little optimisation!
int sherlockAndAnagrams(string s){ int i, j, k, m, len, count = 0; len = s.length(); string a[len], x, y; for(i=1; i<len; i++) { m = len-i; for(j=0; j<=m; j++) a[j] = s.substr(j, i); for(j=0; j<m; j++){ for (k = j + 1; k <= m; k++) { if (a[j] == a[k]) count++; else if(i >= 2){ x = a[j]; y = a[k]; sort(x.begin(), x.end()); sort(y.begin(), y.end()); if (x == y) count++; } } } } return count; }
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 →
Inspired from etayluz clue but with little optimisation!