The string is called isomorphic if the letters of the one string can be mapped to get the second string. Mapping means replacing all occurrences of a letter with another letter but the ordering of the letters remains unchanged.
Given two strings s and t, determine if they are isomorphic.
Two strings s and t 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.
Example 1:
Input: s = "egg", t = "add"
Output: trueExample 2:
Input: s = "foo", t = "bar"
Output: falseExample 3:
Input: s = "paper", t = "title"
Output: trueSOLUTION (IN JAVA)
class Solution {
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] sMapping = new int[256];
int[] tMapping = new int[256];
for (int i = 0; i < s.length(); i++) {
char c1 = s.charAt(i);
char c2 = t.charAt(i);
if (sMapping[c1] != tMapping[c2]) {
return false;
}
sMapping[c1] = i + 1;
tMapping[c2] = i + 1;
}
return true;
}
}
-OSHO UPADHYAY



