forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0205-isomorphic-strings.c
35 lines (33 loc) · 1.05 KB
/
0205-isomorphic-strings.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
/*
Given two strings s and t, determine if they are isomorphic.
Space: O(1)
Time: O(n)
*/
bool isIsomorphic(char * s, char * t){
int alphabet_s[256]; // Alphabet of t letters to s
int alphabet_t[256]; // Alphabet of s letters to t
for (int i=0; i<256; i++){ // Fill alphabets with empty values
alphabet_s[i] = -1;
alphabet_t[i] = -1;
}
int i; // To be able to use it outside the loop for
for (i=0; s[i]!='\0'; i++) {
if (alphabet_t[s[i]]==-1 && alphabet_s[t[i]]==-1) {
alphabet_t[s[i]] = t[i];
alphabet_s[t[i]] = s[i];
} else if (alphabet_t[s[i]]==-1) {
if (alphabet_s[t[i]] != s[i]) {
return false;
}
alphabet_t[s[i]] = t[i];
} else if (alphabet_s[t[i]]==-1) {
if (alphabet_t[s[i]] != t[i]) {
return false;
}
alphabet_s[t[i]] = s[i];
} else if (alphabet_t[s[i]] != t[i] || alphabet_s[t[i]] != s[i]) {
return false;
}
}
return t[i]=='\0';
}