-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisomorphic-strings.cpp
More file actions
60 lines (49 loc) · 1.07 KB
/
Copy pathisomorphic-strings.cpp
File metadata and controls
60 lines (49 loc) · 1.07 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
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
bool areIsomorphic(string str1, string str2)
{
int m = str1.length();
int n = str2.length();
if(n != m)
return false;
bool marked[1000010] = {false};
int map[1000010];
memset(map, -1, sizeof(map));
for (int i = 0; i < n; i++)
{
// if character is seen for the first time
if(map[str1[i]] == -1)
{
// then current char of str2 must have not appeared before
if (marked[str2[i]])
return false;
// mark currenct char of str2 as visited
marked[str2[i]] = true;
// and store mapping or current chars
map[str1[i]] = str2[i];
}
// if not first appeance in str1 then check if previous appearence mapped to
// same char of str2
else if(map[str1[i]] != str2[i])
return false;
}
return true;
}
int main()
{
int aux;
string str1;
string str2;
cin >> str1;
cin >> str2;
if (areIsomorphic(str1,str2))
{
printf("Sim\n");
} else {
printf("Nao\n");
}
return 0;
}