FastPrepText Pattern Matching with Stars

Text Pattern Matching with Stars

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given a lowercase string text and a pattern pattern containing lowercase letters and the wildcard *.

  • A lowercase letter matches the same letter.
  • A * matches any sequence of characters, including the empty sequence.

Return true if the entire pattern matches the entire text. Otherwise, return false.

Consecutive stars have the same meaning as a single star.

Function

matchesTextPattern(text: String, pattern: String) → boolean

Examples

Example 1

text = "adceb"pattern = "*a*b"return = true

The first star matches the empty prefix, and the second star matches dce.

Example 2

text = "acdcb"pattern = "a*c*d"return = false

The final d cannot match the final b, regardless of how the stars expand.

Example 3

text = "abc"pattern = "***"return = true

The consecutive stars collectively match the complete text.

Constraints

  • 0 <= text.length <= 200000.
  • 1 <= pattern.length <= 200000.
  • text contains only lowercase English letters.
  • pattern contains only lowercase English letters and *.

More Headlands Technologies problems

See Headlands Technologies hiring insights
public boolean matchesTextPattern(String text, String pattern) {
    // Write your code here.
}
text"adceb"
pattern"*a*b"
expectedtrue
Checking account…