-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasy_Prob176.cpp
More file actions
76 lines (62 loc) · 1.57 KB
/
Easy_Prob176.cpp
File metadata and controls
76 lines (62 loc) · 1.57 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
/* Given two rectangles on a 2D graph, return the area of their intersection. If the rectangles don't intersect, return 0. */
#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include<vector>
#include <utility>
using namespace std;
bool cmp(pair<char, int>& a,
pair<char, int>& b) {
return a.second < b.second;
};
void sortMap(map<char, int>& M) {
vector<pair<char, int> > A;
for (auto& it : M) {
A.push_back(it);
}
sort(A.begin(), A.end(), cmp);
};
bool isMappedString(string str1, string str2) {
map<char, int> dict1;
map<char, int> dict2;
bool isMapped = true;
for (char c : str1) {
if (dict1.count(c) == 0) {
dict1[c] = 0;
} else {
dict1[c] += 1;
}
}
for (char c : str2) {
if (dict2.count(c) == 0) {
dict2[c] = 0;
} else {
dict2[c] += 1;
}
}
sortMap(dict1);
sortMap(dict2);
if (dict1.size() == dict2.size()) {
map<char,int>::iterator it1 = dict1.begin();
map<char,int>::iterator it2 = dict2.begin();
for (int i=0; i<dict1.size(); i++){
if (it1->second != it2->second) {
isMapped = false;
break;
}
it1 ++;
it2 ++;
}
} else {
isMapped = false;
}
return isMapped;
};
int main(int argc, char *argv[])
{
string str1 = "abs";
string str2 = "cdb";
string str3 = "abb";
cout << isMappedString(str1, str2) << endl;
cout << isMappedString(str1, str3) << endl;
};