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! 🌷
Examples
01 · Example 1
pattern = "abab" s = "redblueredblue" return = true
One possible mapping is as follows:
- 'a' -> "red"
- 'b' -> "blue"
02 · Example 2
pattern = "aaaa" s = "asdasdasdasd" return = true
One possible mapping is as follows:
- 'a' -> "asd"
03 · 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 <= 20patternandsconsist of only lowercase English letters.
More Microsoft problems
- Rank Open BusinessesPHONE SCREEN · Seen May 2026
- Retain Top K ValuesPHONE SCREEN · Seen May 2026
- In-Memory SQL with CSV InitializationONSITE INTERVIEW · Seen May 2026
- Order Records by Matching Start and EndONSITE INTERVIEW · Seen May 2026
- Recover Corrupted Master PageONSITE INTERVIEW · Seen Feb 2026
- Distinct Number Line MovesOA · Seen Oct 2025
- Minimum Round Trip LengthsOA · Seen Aug 2025
- Programmer StringsOA · Seen Aug 2025
public boolean wordPatternMatch(String pattern, String s) {
// write your code here
}
pattern"abab"
s"redblueredblue"
expectedtrue
sign in to submit