• 0

Determine if given strings are isomorphic

Problem :

Given two (dictionary) words as Strings, determine if they are isomorphic. Two words are called isomorphic
if the letters in one word can be remapped to get the second word. Remapping a letter means replacing all
occurrences of it with another letter while the ordering of the letters remains unchanged. No two letters
may map to the same letter, but a letter may map to itself.


given "foo", "app"; returns true 
we can map 'f' -> 'a' and 'o' -> 'p' 
given "bar", "foo"; returns false 
we can't map both 'a' and 'r' to 'o' 

"turtle", "tletur"; returns true 
we can map 't' -> 't', 'u' -> 'l', 'r' -> 'e', 'l' -> 'u', 'e' -'r' 

given "ab", "ca"; returns true 
we can map 'a' -> 'c', 'b'

Logic :

  • Check length of both the strings
    • If strings are of not equal length then return false
  • Create a character map use characters from string 1 as key and map it with string 2
  • While iterating over the string if string 1's character does not exist in the hash map then add it to the hash map and map it to the string 2's value
  • If it exists then compare it with the string 2's character

Solution :