BackeasyHashing Amazon

Isomorphic Strings Solution

Problem Statement

Two ancient scrolls are suspected to use the same cipher. Determine if the letter mapping from one scroll's text to the other is a valid one-to-one cipher (isomorphic).

Example 1
Input
s1 = 'egg', s2 = 'add'
Output
true

Explanation: Step-by-step: 1. Create a hashmap to store the mapping of characters from s1 to s2. 2. Iterate through s1 and s2 simultaneously. 3. For each pair of characters, check if the character in s1 is already in the hashmap. If it is, check if the mapped character in s2 matches the current character in s2. If it does not match, return false. 4. If the character in s1 is not in the hashmap, add it to the hashmap with the current character in s2 as its value. 5. After iterating through both strings, return true if no mismatches were found.

Example 2
Input
s1 = 'foo', s2 = 'bar'
Output
true

Explanation: Step-by-step: 1. Create a hashmap to store the mapping of characters from s1 to s2. 2. Iterate through s1 and s2 simultaneously. 3. For each pair of characters, check if the character in s1 is already in the hashmap. If it is, check if the mapped character in s2 matches the current character in s2. If it does not match, return false. 4. If the character in s1 is not in the hashmap, add it to the hashmap with the current character in s2 as its value. 5. After iterating through both strings, return true if no mismatches were found.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • t.length == s.length
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free