-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisomorphic(nlogn).cpp
More file actions
35 lines (35 loc) · 954 Bytes
/
isomorphic(nlogn).cpp
File metadata and controls
35 lines (35 loc) · 954 Bytes
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
#include<bits/stdc++.h>
using namespace std;
bool is_isomorphic(string input1, string input2)
{
int n = input1.length();
if(n!=input2.length()) return false;
map<char,char> s;
map<char,int> exclusive;
s.insert(pair<char,char>(input1[0],input2[0]));
exclusive.insert(pair<char,int>(input2[0],1));
for(int i =1;i<n;i++){
if(s.count(input1[i])){
if(input2[i] != s.find(input1[i])->second){
return false;
}
}else if(s.count(input1[i])==0){
if(exclusive.count(input2[i])) return false;
else{
s.insert(pair<char,char>(input1[i],input2[i]));
exclusive.insert(pair<char,int>(input2[i],1));
}
}
}
return true;
}
int main() {
int t;cin>>t;
while(t--){
string s1,s2;
cin>>s1;cin>>s2;
cout<<is_isomorphic(s1,s2);
cout<<"\n";
}
return 0;
}