FastPrepIsomorphic Strings

Isomorphic Strings

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given two strings first and second, return true when characters of first can be replaced to obtain second.

Every occurrence of one source character must map to the same target character, and two different source characters may not map to the same target character. Character order is preserved.

Function

areIsomorphic(first: String, second: String) → boolean

Examples

Example 1

first = "egg"second = "add"return = true

e maps to a and g maps to d.

Example 2

first = "foo"second = "bar"return = false

o would need to map to both a and r.

Example 3

first = "aaa"second = "XYZ"return = false

The same source character cannot map to three targets.

Constraints

  • 0 <= first.length, second.length <= 200000.
  • Strings contain ASCII characters.

More Google problems

See Google hiring insights
public boolean areIsomorphic(String first, String second) {
    // Write your code here.
}
first"egg"
second"add"
expectedtrue
Checking account…