FastPrepWord Pattern II (Round 1, LC 291 :)

Word Pattern II (Round 1, LC 291 :)

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a pattern and a string s, return true if s matches the pattern.

A string s matches a pattern if there is some bijective mapping of single characters to non-empty strings such that if each character in pattern is replaced by the string it maps to, then the resulting string is s. A bijective mapping means that no two characters map to the same string, and no character maps to two different strings.

🌷 wallz carries! 🌷

Function

wordPatternMatch(pattern: String, s: String) → boolean

Examples

Example 1

pattern = "abab"s = "redblueredblue"return = true

One possible mapping is as follows:

  • 'a' -> "red"
  • 'b' -> "blue"

Example 2

pattern = "aaaa"s = "asdasdasdasd"return = true

One possible mapping is as follows:

  • 'a' -> "asd"

Example 3

pattern = "aabb"s = "xyzabcxyzabc"return = false

No possible mapping exists that can satisfy the given pattern and string.

Constraints

  • 1 <= pattern.length, s.length <= 20
  • pattern and s consist of only lowercase English letters.

More Microsoft problems

See Microsoft hiring insights
public boolean wordPatternMatch(String pattern, String s) {
  // write your code here
}
pattern"abab"
s"redblueredblue"
expectedtrue
Checking account…