-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-anagram.cpp
More file actions
77 lines (73 loc) · 1.82 KB
/
Copy pathstring-anagram.cpp
File metadata and controls
77 lines (73 loc) · 1.82 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Check if two strings are anagrams
#include <iostream>
//#include <chrono> //uncomment for time
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
//using namespace std::chrono;
// brute force
bool anagram1(string s1, string s2) // O(n log n)
{
sort(s1.begin(), s1.end()); // O(n logn)
sort(s2.begin(), s2.end());
return (s1 == s2); // O(n)
}
// hash map (for finite character sets)
typedef unordered_map<char, unsigned int> hashmap;
bool anagram2(string s1, string s2) // O(n)
{
hashmap string_rep;
// increment frequency count for string 1 chars
for (unsigned int i = 0; s1[i]; i++) // O(n)
{
if (!string_rep.count(s1[i])) //O(1)
string_rep[s1[i]] = 1;
else
string_rep[s1[i]]++;
}
// decrement frequency for each char in string 2
for (unsigned int i = 0; s2[i]; i++) // O(n)
{
if (!string_rep.count(s2[i])) //O(1)
return false; // not present in map
else
string_rep[s1[i]]--;
}
//search for non-zero values
for (auto i : string_rep) // O(n)
{
if (i.second)
return false;
}
return true;
}
int main()
{
string s1, s2; //case sensitive
cin >> s1 >> s2;
//auto start = high_resolution_clock::now();
if (s1.length() != s2.length())
cout << "not anagram";
if (anagram1(s1, s2)) //replace X by 1 or 2
cout << "anagram";
else
cout << "not anagram";
//auto stop = high_resolution_clock::now();
//auto duration = duration_cast<microseconds>(stop - start);
//cerr << "\ntime (µs): " << duration.count();
return 0;
}
/*
output with example:
car acr => anagram
car rcaa => not anagram
algo1:
car --sorted--> acr
rca --sorted--> acr == acr
rcaa --sorted--> aacr != acr
algo2:
map+(car): (c->1,a->1,r->1)
map-(rca): (c->0,a->0,r->0) => all zero
map-(rcaa): (r->0,c->0,a->-1) => non-zero present
*/