v9.1.1
Menu

Isomorphic Strings

easyFREEHash Maps & CountingLinkedIn DSA

Two strings s and t are isomorphic if the characters in s can be replaced to get t, preserving order and with a one-to-one mapping.

Return True or False.

Example

Input
egg
add
Output
True
Reasoning
  • We create two empty mappings to store the character relationships between s and t.
  • We iterate over the characters in s and t simultaneously, checking if each character in s maps to a unique character in t. In this case, 'e' maps to 'a', 'g' maps to 'd', and the second 'g' still maps to 'd'.
  • Since the mappings are consistent ('e' to 'a' and 'g' to 'd') and one-to-one, we can conclude that s and t are isomorphic.
  • The final output is True, indicating that the strings are isomorphic.

Constraints

  • 1 <= len(s) == len(t) <= 5 * 10^4
  • s and t consist of ASCII characters

Learn this in

More from LinkedIn DSA

solution.py

Test results

Sign in to run your code against the test cases.