Strings String basics

127. Isomorphic Strings

Easy

You are given two strings s and t. Return true if you can turn s into t by replacing its letters, and false otherwise. Two such strings are called isomorphic.

The replacing follows two rules. Every copy of a letter is replaced by the same letter. Two different letters are never replaced by the same letter. A letter may also stay as it is.

The letters keep their places, so the letter at index i of s becomes the letter at index i of t.

Example 1

Input: s = "moon", t = "seed"

Output: true

Explanation: m becomes s, both copies of o become e, and n becomes d. No two letters of s share a replacement.

Example 2

Input: s = "bat", t = "too"

Output: false

Explanation: a and t would both have to become o. Two different letters cannot become the same letter.

Example 3

Input: s = "abab", t = "baba"

Output: true

Explanation: a becomes b and b becomes a. A letter may become a letter that s also uses.

Check your understanding

Work out the answer for this input before you write any code.

s = "aab", t = "xyz"

Constraints

  • 1 ≤ s.length, t.length ≤ 105
  • s and t hold lowercase English letters only.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.