class Solution {
public:
bool isIsomorphic(string s, string t) {
int m1[256] = {0}, m2[256] = {0}, n = s.size();
for (int i = 0; i < n; ++i) {
if (m1[s[i]] != m2[t[i]]) return false;
m1[s[i]] = i + 1;
m2[t[i]] = i + 1;
}
return true;
}
};
My 6 lines solution

Nice code. To prevent overflow (as commented by Debanjan) we can add some extra code:
bool isIsomorphic(string s, string t) { int m1[256]={0}, m2[256]={0}; int count = 1; for(int i=0; i<s.length(); i++) { if(m1[s[i]]!=m2[t[i]]) return false; if(m1[s[i]]==0) { m1[s[i]]=count; m2[t[i]]=count; count++; } } return true; }

Is this clear?
"No two characters may map to the same character but a character may map to itself."
I thought while "egg" map to "add", "egg" shall not map to "agg" because they are same charactors and only appear twice, while "eggg" has no problem map to "aggg" since even the repeatable charactors are same, there are 3 of them, not equal to 2.

The "i + 1" made me a bit confused, then realized "+1" is to exclude 0 from valid index. Why not initialzing the array as 1, and simply keep i in the array. This also avoids the overflow issue. AC code as below:
class Solution { public: bool isIsomorphic(string s, string t) { int len = s.length(); int m1[256], m2[256]; for (int i = 0; i < 256; i++) { m1[i] = m2[i] = 1; } for (int i = 0; i < len; i++) { if (m1[s[i]] != m2[t[i]]) return false; m1[s[i]] = m2[t[i]] = i; } return true; } };

// Just a below5ms C solution for reference or optimization ( thx ).
bool isIsomorphic(char* s, char* t) {
int length = strlen(s); int arr[128]; int arr2[128]; for( int j=0; j<128; j++){ arr[j] = 1; arr2[j] = 1; } for( int i = 0; i<length; i++){ if( arr[ s[i] ] == 1 && arr2[ t[i] ] == 1 ){ arr[ s[i] ] = t[i]; arr2[ t[i]] = s[i]; }else if( arr[ s[i] ] != t[i]  arr2[ t[i] ] != s[i] ) return false; else continue; } return true;
}

great. mine may be less simple and elegant, but more intuitive and still simple.
intuitively two map needed, so I use two array,
a mapper and a reverse mapper.class Solution { public: bool isIsomorphic(string s, string t) { int mapper[256]={0}, rmapper[256]={0}; int i, len=s.size()==t.size()?s.size():1; // to handle unequal length for(i=0;i<len;++i) { if(mapper[s[i]]) { if(t[i]!=mapper[s[i]]) return false; } // s[i] already mapped else { if(!rmapper[t[i]]) { mapper[s[i]]=t[i]; rmapper[t[i]]=s[i]; } // t[i] not mapped yet else if(s[i]!=rmapper[t[i]]) return false; // t[i] already mapped } } return len!=1; } };

Well, the code can be simplified much.
 Initializing to
0
will enable collective initialization and save code;  For Cstyle
char*
, there is no need to computestrlen(s)
becauses[i]
will just tell us whether this is the end of the string\0
.
The code is as follows: 5 lines and 0 ms.
bool isIsomorphic(char* s, char* t) { int sp[256] = {0}, tp[256] = {0}; for (int i = 0; s[i]; i++) { if (sp[s[i]] != tp[t[i]]) return false; sp[s[i]] = tp[t[i]] = i + 1; } return true; }
 Initializing to

@jianchao.li.fighter, that's true, it would be faster to handle arrays. Both will be constant space, correct but my point is that in several cases, 256 characters will not be used and so this could be considered a waste of space. But then 256 spaces of memory isn't much, so no big deal I guess.