forked from vaibhavpathak999/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram_search.cpp
More file actions
46 lines (41 loc) · 1.11 KB
/
Copy pathanagram_search.cpp
File metadata and controls
46 lines (41 loc) · 1.11 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
/*
* Checks if two strings are anagrams of each other, ignoring any whitespace.
*
* Create maps of the counts of every character in each string.
* As well as keep a set of all characters used in both strings.
*
* Check to ensure every unique character is used in both strings the
* same number of times.
*/
vector<int> getMap(const string& s)
{
vector<int> m(26, 0);
for(char c : s)
{
if(c != ' ')
{
c = tolower(c);
m[c - 'a']++;
}
}
return m;
}
bool isAnagram(const string& s1, const string& s2)
{
vector<int> m1 = getMap(s1);
vector<int> m2 = getMap(s2);
return m1 == m2;
}
int main()
{
cout << isAnagram("anagram", "not a gram") << endl; // false
cout << isAnagram("anagram", "na a marg") << endl; // true
cout << isAnagram("William Shakespeare", "I am \t a weakish speller") << endl; // true
cout << isAnagram("Madam Curie", "Radium came") << endl; // true
cout << isAnagram("notagram", "notaflam") << endl; // false
return 0;
}