forked from codingCapricorn/Hactoberfest-2020-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_anagram.cpp
59 lines (54 loc) · 1.31 KB
/
valid_anagram.cpp
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
#include <bits/stdc++.h>
class Anagram {
public:
bool isAnagram(string s, string t) {
map <char,int> hash;
map <char,int> hash1;
if(s.size()==t.size())
{
for(int i=0;i<s.size();i++)
{ if(hash.find(s[i])==hash.end())
hash.insert(pair<char, int>(s[i],1));
else
hash[s[i]]++;
}
for(int i=0;i<s.size();i++)
{ if(hash1.find(t[i])==hash1.end())
hash1.insert(pair<char, int>(t[i],1));
else
hash1[t[i]]++;
}
for(auto itr:hash1)
{
cout<<itr.first<<itr.second<<"\n";
}
if(hash1.size()==hash.size())
{
for (auto itr:hash)
{
if(hash1.find(itr.first)==hash1.end())
return false;
}
for (auto itr:hash)
{
auto i=itr.first;
if(hash1[i]!=itr.second)
return false;
}
return true;
}
else
return false;
}
else
{
return false;
}
}
};
int main()
{
Anagram obj;
bool output=obj.isAnagram("anagram", "nagaram"");
cout<<output;
return 0;}