-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.c
42 lines (41 loc) · 942 Bytes
/
anagram.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//Check whether the strings are anagram or not using hashing | Vishruth codes
#include<stdio.h>
int main()
{
int count=0;
char str1[]="stressed";
char str2[]="dessert";
printf("\nThe 2 strings are %s & %s.",str1,str2);
int hash[26]={0};
int j=0;
for(int i=0;str1[i]!='\0';i++)
{
hash[str1[i]-97]+=1;
}
for(j=0;str2[j]!='\0';j++)
{
hash[str2[j]-97]-=1;
if(hash[str2[j]-97]<0)
{
//if <0, that word in str2 wasn't present in str1, hence not anagram
printf("\nNot an anagram.");
break;
}
}
//For presence of duplicate elements
for(int i=0;i<26;i++)
{
if(hash[i]>0 || hash[i]<0)
{
count++;
}
}
if(count==0)
{
printf("\nIt's an anagram.");
}
else{
printf("\nNot an anagram.");
}
return 0;
}