-
Notifications
You must be signed in to change notification settings - Fork 0
/
isomorphic_strings.cpp
40 lines (32 loc) · 1022 Bytes
/
isomorphic_strings.cpp
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
/*
Given two strings 'str1' and 'str2', check if these two strings are isomorphic to each other.
Two strings str1 and str2 are called isomorphic if there is a one to one mapping possible for every character of str1 to every character of str2 while preserving the order.
Note: All occurrences of every character in ‘str1’ should map to the same character in ‘str2’
*/
#include <bits/stdc++.h>
using namespace std;
bool isIsomorphic(string a, string b){
if(a.length() != b.length()) return false;
int h1[26] = {}, h2[26] = {};
for(int i = 0; i < a.length(); i++){
//if both character did not appearh before
//add them to hashmaps
if(h1[a[i]-'a'] == 0 && h2[b[i]-'a'] == 0){
h1[a[i]-'a'] = b[i];
h2[b[i]-'a'] = a[i];
}
//else if their hashmap does not match return false
else if(h1[a[i]-'a'] != b[i] || h2[b[i]-'a'] != a[i])
return false;
}
return true;
}
int main(){
string a, b;
cin >> a >> b;
if(isIsomorphic(a, b)){
cout << "TRUE\n";
}
else cout << "FALSE\n";
return 0;
}