-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhas_character_map.cpp
More file actions
68 lines (51 loc) · 1.26 KB
/
has_character_map.cpp
File metadata and controls
68 lines (51 loc) · 1.26 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
//
// Created by Mayank Parasar on 2020-03-21.
//
/*
Given two strings, find if there is a one-to-one mapping of characters between the two strings.
Example
Input: abc, def
Output: True # a -> d, b -> e, c -> f
Input: aab, def
Ouput: False # a can't map to d and e
Here's some starter code:
def has_character_map(str1, str2):
# Fill this in.
print(has_character_map('abc', 'def'))
# True
print(has_character_map('aac', 'def'))
# False
*/
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<char, char> mymap;
bool has_character_map(string str1, string str2) {
if(str1.length() != str2.length())
return false;
else {
for(int ii=0, jj = 0; ii<str1.length(), jj<str2.length(); ii++, jj++) {
cout << str1[ii] << " " << str2[jj] << endl;
if(mymap.find(str1[ii]) == mymap.end()){
mymap[str1[ii]] = str2[jj];
}
else {
return false;
}
}
}
return true;
}
int main() {
string str1 = "abc";
string str2 = "def";
string str3 = "aac";
string str4 = "def";
cout << boolalpha;
cout << has_character_map(str1, str2);
cout << endl;
cout << has_character_map(str3, str4);
cout << endl;
return 0;
}