bool areIsomorphic(string &str1, string &str2)
{
string done = "";
done += str2[0]; // Append the first character of str2 to done
string visitstr1 = "";
visitstr1 += str1[0]; // Append the first character of str1 to visitstr1 (maintains visited characters)
if (str1.length() != str2.length()) {
return false; // Lengths are different, not isomorphic
}
for (int i = 1; i < str1.length(); i++) {
if (visitstr1.find(str1[i]) != string::npos) {
if (done.find(str2[i]) == string::npos) {
return false; // The character is already visited in str1 but received a different character in str2
}
}
else {
if (done.find(str2[i]) != string::npos) {
return false; // Received the same character which is already assigned to a character of str1
}
else {
done += str2[i]; // Received a different character in str2 for the respective character in str1
}
}
visitstr1 += str1[i]; // Add character str1[i] to visitstr1 as visited }
return true; // Strings are isomorphic
}