Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg"
, "add"
, return true.
Given "foo"
, "bar"
, return false.
Given "paper"
, "title"
, return true.
Note:
You may assume both s and t have the same length.
1 bool isIsomorphic(char* s, char* t) { 2 if(s == NULL || t == NULL) 3 return false; 4 int slen = strlen(s); 5 int tlen = strlen(t); 6 if(slen != tlen) 7 return false; 8 int index; 9 int s_char_content[256]; 10 int t_char_content[256]; 11 for(index = 0; index < 256 ;index++) 12 { 13 s_char_content[index] = 257; 14 t_char_content[index] = 257; 15 } 16 17 for(index = 0; index < slen; index++) 18 { 19 if(s_char_content[s[index]] == 257) 20 { 21 if(t_char_content[t[index]] == 257) 22 { 23 s_char_content[s[index]] = t[index]; 24 t_char_content[t[index]] = s[index]; 25 } 26 else return false; 27 } 28 else 29 { 30 if(s_char_content[s[index]] != t[index]) 31 return false; 32 } 33 } 34 return true; 35 }