-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram.cpp
More file actions
54 lines (46 loc) · 885 Bytes
/
anagram.cpp
File metadata and controls
54 lines (46 loc) · 885 Bytes
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
/**
* @file anagram.cpp
* @author Abhishek
* @brief Below program checks if the given two strings are anagram or not.
* @version 0.1
* @date 2022-04-21
*
* @copyright Copyright (c) 2022
*
*/
#include <iostream>
bool isAnagram(char *s1, char *s2)
{
int count1 = 0, count2 = 0;
for(int i = 0; s1[i] != '\0'; i++)
{
count1++;
}
for(int i = 0; s2[i] != '\0'; i++)
{
count2++;
}
if(count1 != count2)
return false;
int arr[26] = {0};
for(int i = 0; s1[i] != '\0'; i++)
{
arr[s1[i]-97]++;
arr[s2[i]-97]--;
}
for(int i = 0; i < 26; i++)
{
if(arr[i] != 0)
{
return false;
}
}
return true;
}
int main(int argc, char const *argv[])
{
char str[] = "aa";
char str2[] = "bb";
std::cout << isAnagram(str, str2);
return 0;
}