-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path205-isomorphic-strings.cpp
More file actions
81 lines (74 loc) · 2.42 KB
/
205-isomorphic-strings.cpp
File metadata and controls
81 lines (74 loc) · 2.42 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
78
79
80
81
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
// performance measure header
#include <iomanip>
#include <chrono>
using namespace std;
// https://leetcode.com/problems/isomorphic-strings/
// sample 4ms solution with maps
/* class Solution {
public:
bool isIsomorphic(string s, string t) {
int n = s.size();
unordered_map<char, int> map1;
unordered_map<char, int> map2;
for(int i=0; i<n; i++){
if(map1[s[i]] != map2[t[i]]){
return false;
}
map1[s[i]] = map2[t[i]] = i+1;
}
return true;
}
}; */
// my proposed solution
class Solution {
public:
bool isIsomorphic(string s, string t) {
vector<int> map_s, map_t;
string s_unique {s};
string t_unique {t};
s_unique.erase(unique(s_unique.begin(), s_unique.end()), s_unique.end());
t_unique.erase(unique(t_unique.begin(), t_unique.end()), t_unique.end());
for (char ch : s_unique) {
int counter {0};
for (size_t i = 0; (i = s.find(ch, i)) != string::npos; i++) {
counter++;
}
map_s.push_back(counter);
}
for (char ch : t_unique) {
int counter {0};
for (size_t i = 0; (i = t.find(ch, i)) != string::npos; i++) {
counter++;
}
map_t.push_back(counter);
}
return map_s == map_t ? true : false;
}
};
int main() {
Solution solution;
string s, t;
getline(cin, s);
getline(cin, t);
// performance check begin
auto t1 {chrono::high_resolution_clock::now()};
bool isIsomorphic {solution.isIsomorphic(s, t)};
auto t2 {chrono::high_resolution_clock::now()};
// milliseconds ms
chrono::duration<double, milli> ms_double {t2 - t1};
cout << setprecision(17) << ms_double.count() << " ms\n";
// microseconds us
chrono::duration<double, micro> us_double {t2 - t1};
cout << setprecision(17) << us_double.count() << " us\n";
// nanoseconds ns
chrono::duration<double, nano> ns_double {t2 - t1};
cout << setprecision(17) << ns_double.count() << " ns\n";
// performance check end
cout << isIsomorphic << '\n';
return 0;
}