Power of string is the sum of square of frequency of different letters in the string. Given string only consists of lowercase English letters. Your task is to find the power of string.
Input Format
First line contains an integer for the number of test case. Each test case consists of length of the input string and the string.
Constraints
1 <= t <= 10. Input size of string length over all the test cases doesn't exceed 100000.
Output Format
For each test case print the power of string in a new line.
Sample Input 0
1
5
aabbc
Sample Output 0
9
Source code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
char str[n];
scanf("%s",str);
long int a[26]={0};
int i=0;
while(str[i])
{
a[str[i]-97]++;
i++;
}
long long int sum=0;
for(i=0;i<26;i++)
{
if(a[i]!=0)
{
sum+=pow(a[i],2);
}
}
printf("%lld\n",sum);
}
return 0;
}
Comments
Post a Comment