Isomorphic Strings
Solution to Isomorphic Strings problem.
// Reading this helps - https://www.educative.io/edpresso/how-to-check-if-two-strings-are-isomorphic
class Solution {
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length())
return false;
Map<Character, Character> mapping = new HashMap<>();
Set<Character> charsAlreadyUsedForMapping = new HashSet<Character>();
for(int i=0;i<s.length();i++) {
char sc = s.charAt(i);
char tc = t.charAt(i);
// already a mapping exists for this char
if (mapping.get(sc) != null)
{
if (mapping.get(sc) != tc) {
// the exiting mapping is not same as required mapping in t
return false;
}
}
else // no mapping exists
{
// if the char is already mapped you cannot use it again
if (charsAlreadyUsedForMapping.contains(tc)) {
return false;
}
mapping.put(sc, tc);
charsAlreadyUsedForMapping.add(tc);
}
}
return true;
}
}