-
Notifications
You must be signed in to change notification settings - Fork 118
/
205.c
51 lines (40 loc) · 1.03 KB
/
205.c
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
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
bool isIsomorphic(char* s, char* t) {
int len = strlen(s); /* same length */
char hashs[128] = { 0 }; /* for ascii code */
char hasht[128] = { 0 };
int i;
for (i = 0; i < len; i++) {
int x = s[i];
if (hashs[x] == 0) {
hashs[x] = t[i];
}
else {
if (hashs[x] != t[i]) {
return false;
}
}
int y = t[i];
if (hasht[y] == 0) {
hasht[y] = s[i];
}
else {
if (hasht[y] != s[i]) {
return false;
}
}
}
return true;
}
int main() {
assert(isIsomorphic("egg", "add") == true);
assert(isIsomorphic("foo", "bar") == false);
assert(isIsomorphic("paper", "title") == true);
assert(isIsomorphic("bar", "foo") == false);
assert(isIsomorphic("13", "42") == true);
printf("success!\n");
return 0;
}