# Sherlock and Anagrams Hackerrank problem solution

##### Sherlock and Anagrams Hackerrank

Given a string S, find the number of `"unordered anagrammatic pairs"` of substrings.

Input Format
First line contains T, the number of testcases. Each testcase consists of string S in one line.
Constraints

1<=T<=10
1<=length(S)<=100
String S contains only the lowercase letters of the English alphabet.
Output Format
For each testcase, print the required answer in one line.

``````<strong>Sample Input#00</strong>

2
abba
abcd

<br>

<strong>Sample Output#00</strong>

4
0

<br>

Sample Input#01

5
ifailuhkqq
hucpoltgty
ovarjsnrbf
pvmupwjjjf
iwwhrlkpek

<br>
<strong>
Sample Output#01</strong>
3
2
2
6
3``````

##### Solution

``````#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int check_anagram(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c = 0;

while (a[c] != '\0') {
first[a[c]-'a']++;
c++;
}
c = 0;
while (b[c] != '\0') {
second[b[c]-'a']++;
c++;
}

for (c = 0; c < 26; c++) {
if (first[c] != second[c])
return 0;
}

return 1;
}

int main() {
int t;
scanf("%d", &t);
while (t--) {
char s[100];
char sub1[100] = {0};
char sub2[100] = {0};
scanf("%s", s);

int count = 0;
for (int len = 1; len < strlen(s); len++) {
memset(sub1, 0, len);
for (int i = 0; i < strlen(s) - len; i++) {
strncpy(sub1, &s[i], len);
memset(sub2, 0, len);
for (int j = i + 1; j < strlen(s) - len + 1; j++) {
strncpy(sub2, &s[j], len);
if (check_anagram(sub1, sub2) == 1) {
count++;
}
}
}
}

printf("%d\n", count);

}
return 0;
}
``````
``````from collections import *
for i in range(input()):
s = raw_input()
check = defaultdict(int)
l = len(s)
for i in range(l):
for j in range(i+1,l+1):
sub = list(s[i:j])
sub.sort()
sub = "".join(sub)
check[sub]+=1
sum = 0
for i in check:
n = check[i]
sum += (n*(n-1))/2
print sum``````
``````import java.io.*;
import java.util.*;
public class Solution
{
public static void main(String[] args)
{
int m,n;
String s="",temp="";
Scanner in = new Scanner(System.in);
m = in.nextInt();
while(m!=0)
{
s = in.next();
n = s.length();
int p = 0,t=0;
int z = (n*n)-(((n*n)-n)/2);
String posi[] = new String[z];
for(int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
if(x<=y)
{
temp = s.substring(x,y+1);
char c[] = temp.toCharArray();
Arrays.sort(c);
temp = new String(c);
posi[p] = temp;
p++;
}
}
}
for(int i=0;i<z;i++)
{
for(int j=i+1;j<z;j++)
{
if(posi[i].equals(posi[j]))
{
t++;
}
}
}
System.out.println(t);
m--;
}
}
}``````

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

### hasectic

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi