Skip to content

Instantly share code, notes, and snippets.

@viveknarang
Created February 8, 2019 07:08
Show Gist options
  • Save viveknarang/225da07e113f20358354732ab97c1c58 to your computer and use it in GitHub Desktop.
Save viveknarang/225da07e113f20358354732ab97c1c58 to your computer and use it in GitHub Desktop.
class Solution {
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) {
return false;
}
Map<Character, Character> aMap = new HashMap<Character, Character>();
Map<Character, Character> bMap = new HashMap<Character, Character>();
for (int i = 0 ; i < s.length() ; i++) {
if (!aMap.containsKey(s.charAt(i))) {
aMap.put(s.charAt(i), t.charAt(i));
bMap.put(t.charAt(i), s.charAt(i));
}
}
if (aMap.size() != bMap.size()) {
return false;
}
for (int i = 0 ; i < s.length() ; i++) {
if (aMap.get(s.charAt(i)) != t.charAt(i)) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment