class Solution { public boolean isIsomorphic(String s, String t) { if(s.length()==1)return true; if(s.length()!=t.length()) return false; //make frequency map and map characters HashMap<Character,Character> map = new HashMap<>(); for(int i = 0; i< s.length();i++){ char ch1 = s.charAt(i); char ch2 = t.charAt(i); if(map.containsKey(ch1)){ //if new and old mapped characters are not equal if(map.get(ch1) != ch2) return false; } //if map doesn't contains key else{ //but map contains value if(map.containsValue(ch2)) return false; map.put(ch1 , ch2); } } return true; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter