Skip to content

Commit 688efb7

Browse files
authored
Merge pull request #1334 from julienChemillier/patch-21
Add 205 in c language
2 parents a614b2f + 732b2d3 commit 688efb7

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

c/205-Isomorphic-Strings.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Given two strings s and t, determine if they are isomorphic.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
bool isIsomorphic(char * s, char * t){
9+
int alphabet_s[256]; // Alphabet of t letters to s
10+
int alphabet_t[256]; // Alphabet of s letters to t
11+
for (int i=0; i<256; i++){ // Fill alphabets with empty values
12+
alphabet_s[i] = -1;
13+
alphabet_t[i] = -1;
14+
}
15+
int i; // To be able to use it outside the loop for
16+
for (i=0; s[i]!='\0'; i++) {
17+
if (alphabet_t[s[i]]==-1 && alphabet_s[t[i]]==-1) {
18+
alphabet_t[s[i]] = t[i];
19+
alphabet_s[t[i]] = s[i];
20+
} else if (alphabet_t[s[i]]==-1) {
21+
if (alphabet_s[t[i]] != s[i]) {
22+
return false;
23+
}
24+
alphabet_t[s[i]] = t[i];
25+
} else if (alphabet_s[t[i]]==-1) {
26+
if (alphabet_t[s[i]] != t[i]) {
27+
return false;
28+
}
29+
alphabet_s[t[i]] = s[i];
30+
} else if (alphabet_t[s[i]] != t[i] || alphabet_s[t[i]] != s[i]) {
31+
return false;
32+
}
33+
}
34+
return t[i]=='\0';
35+
}

0 commit comments

Comments
 (0)