Doubt on leetcode

I have a problem in solving this .please help.In this how to ensure that the order of the letters is the same???

hello @Somasree

see u have to map one character of one string with character in other string.
so what u can do is u can keep track of already mapped character using map.




bool isIsomorphic(string s, string t)
{
	vector<char> mapStoT(127, 0);
	vector<char> mapTtoS(127, 0);
	int len = s.length();
	
	for (int i = 0; i < len; ++i)
	{
		char s_char = s[i], t_char = t[i];
		if (mapStoT[s_char] == 0 && mapTtoS[t_char] == 0)
		{
			mapStoT[s_char] = t_char;
			mapTtoS[t_char] = s_char;
		}
		else if (mapTtoS[t_char] != s_char || mapStoT[s_char] != t_char)
		{
			return false;
		}
	}

	return true;
}

let me know if anything is not clear.

//if (mapStoT[s_char] == 0 && mapTtoS[t_char] == 0)
{
mapStoT[s_char] = t_char;
mapTtoS[t_char] = s_char;
}
else if (mapTtoS[t_char] != s_char || mapStoT[s_char] != t_char)
{
return false;
}//
What is the significance of this lines??
Why 127 is used???

here we are checkinh if both the character are unmapped .
and then we are mapping with each other char of s map to char of t and vice versa

here we are checking if both character are already mapped but the mapping is not consistent.
then simply return false

127 is used to accomodate all characters whose ascii value fall in between [0…126]. u can use any other size bigger than that

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.