-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram242.c
More file actions
63 lines (57 loc) · 1.06 KB
/
anagram242.c
File metadata and controls
63 lines (57 loc) · 1.06 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
bool isAnagram(char* s, char* t) {
int lens = strlen(s);
int lent = strlen(t);
int i ,j;
bool found = false;
if(lens == lent) {
for (i = 0; i <lens; i++) {
found = false;
for (j = 0; j < lens; j++) {
if (s[i] == t[j]) {
t[j] = '5';
found = true;
break;
}
}
if(!found) {
return false;
}
}
return true;
} else {
return false;
}
}
int main() {
char a,b;
scanf("%c",a);
scanf("%c",b);
if( isAnagram(a,b) ) {
printf(" yes it isAnagram \n");
} else {
printf(" no it isAnagram \n");
}
return 0;
}
//the fatest method turn the char to ascii if they have count twice
//that value is 0
bool isAnagram(char* s, char* t) {
int array[26];
int i;
int len_s = strlen(s);
int len_t = strlen(t);
if (len_s != len_t)
return (false);
for (i = 0; i < 26; i++) {
array[i] = 0;
}
for (i = 0; i < len_s; i++) {
array[s[i]-'a']++;
array[t[i]-'a']--;
}
for (i = 0; i < 26; i++) {
if (array[i] != 0)
return (false);
}
return (true);
}