Harry Potter and Magical Spells

It’s the beginning of new academic year in Hogwarts School of Witchcraft and Wizardry and Prof. Snape has given Harry a task to analyse magical spells. During this analysis Harry is supposed to find out the number of different letters that are used in a particular spell. Now it’s not easy for Harry to keep the record of used letters while counting the number of different letters present in the spell. So now Harry turns to you for help. You being a coder; it’s just a matter of few keystrokes for you. Harry will give you a string and your code must print the number of different letters that are used in the given spell. (see sample input/output below for more details)

Input

The first line of input contains an integer T<=500 i.e. the number of test cases. Each of the next T line contains a lowercase word (spell) of not more than 50 letters.

Output

For each test case print the number of unique letters present in that string. Answer each test case in a new line.

Time limit

1 second

Sample Input

1
alohomora

Sample Output

6

Explanation

The word alohomora consists of 6 different alphabets i.e. a, l, o, h, m and r. Hence answer is 6.

@admin: this forum would be perfect with a spoiler tag :smiley:

my Python solution (/!\ SPOILER ALERT, DON’T LOOK AT IT IF YOU DON’T WANNA KNOW !) :

T = int(raw_input().strip())
for t in xrange(T):
    S = raw_input().strip()
    print len(set(S))
1 Like

Mmh, a spoiler tag wouldn’t be bad. I’m just not sure how complex it would be to implement in markdown though.

Below is the link to an O(n) time, O(1) space solution, in C, if you need it.

http://ideone.com/8OWAK6

NB: It spoils only those who follow the link. :smiley:

#include<stdio.h>
#include<string.h>
int main()
{
int t,i,j;
char str[50],x;
scanf("%d",&t);
for(i=0;i<t;i++)
{
fflush(stdin);
scanf("%s",str);
int l=strlen(str),ans=0;
for(x=97;x<=122;x++)
for(j=0;j<l;j++)
if(str[j]==x)
{
ans++;
break;
};
printf("%d\n",ans);
}
return 0;
}

#include<stdio.h>
#include<string.h>

int main()
{ char string[50],i;
int T,count;
scanf("%d",&T);

while(T–)
{count=0;
scanf("%s",string);;
int y= strlen(string);
for(i=97;i<=122;i++)
for(int j=0;j<y;j++)
if(string[j]==i) {count++;break;}
printf("\n%d",count);}
return 0;
}

//