Leetcode Isomorphic Strings solution Java

Tags: ,

Given two strings s and t, determine if they are isomorphic.

Two strings 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.

For example,
Given "egg", "add", return true.

Given "foo", "bar", return false.

Given "paper", "title", return true.

Analysis

We can use a HashTable to map the letter from String1 to String2. 

Let c1, c2 denote the current letter from string1 and string2 respectively.

If c1 is already in the table, and the letter mapped by c1 does not equal to c2, return false.

if c1 is not in the table, we need to check whether c2 has already been mapped or not. 

To make this check faster, we can use another Table map  the letter from c2 to c1.

Java Solution: