FastPrepIsomorphic Strings

Isomorphic Strings

Microsoft logoMicrosoft● EasyINTERNONSITE INTERVIEW
Learn

Problem statement

Given two strings s and t of equal length, determine whether their characters have a bijective mapping.

A valid mapping must satisfy both rules:

  • Every occurrence of one character in s maps to the same character in t.
  • Two different characters in s cannot map to the same character in t.

Character positions do not change, and a character may map to itself.

Function

isIsomorphic(s: String, t: String) → boolean

Examples

Example 1

s = "paper"t = "title"return = true

The repeated-character pattern matches: p maps to t, a to i, e to l, and r to e.

Example 2

s = "foo"t = "bar"return = false

The two occurrences of o would need to map to both a and r, which is inconsistent.

Example 3

s = "badc"t = "baba"return = false

Although each character from s is individually consistent, both b and d would map to b, violating bijection.

Constraints

  • 1 <= s.length == t.length <= 50000
  • s and t contain ASCII characters.

More Microsoft problems

See Microsoft hiring insights
public boolean isIsomorphic(String s, String t) {
    // Write your code here.
}
s"paper"
t"title"
expectedtrue
Checking account…