Isomorphic Strings

Easy · Arrays & Hashing · Hash Table · String

Problem

You are given two strings s and t of the same length. They are isomorphic when the characters of s can be replaced to turn s into t under one consistent rule:

Return true if s and t are isomorphic, otherwise false.

Examples

Input: s = "noon", t = "deed"
Output: true

n becomes d and o becomes e everywhere, and no two letters share an image.

Input: s = "abc", t = "xyx"
Output: false

a and c would both have to become x.

Input: s = "aa", t = "ab"
Output: false

The same a would have to become a at one place and b at another.

Constraints

Approaches

Follow-up

Another way to say it: two strings are isomorphic when they have the same shape, where the shape replaces each character by the index of its first appearance ("paper" and "title" both become 0,1,0,3,4). Comparing shapes extends directly to grouping many strings by pattern, as the second use case below does.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…