Miscellaneous problems · Exercise 85 of 100

Match a basic regular expression

In a basic pattern, a letter matches itself and a dot matches any one character. A star after either one allows zero or more copies of that match. Try repetition counts that leave enough characters for the rest of the pattern, and accept a full match only when the text and pattern both run out.

Try it yourself

To match the entire text abbbc with the pattern ab*bc, how many b characters must the b* part consume?

One-step hint

Mark off the literal prefix and suffix of the pattern at the ends of the text. Count the b characters left between those marks.

Worked solution
  1. The initial a matches the first letter. The final bc in the pattern needs the last b and the final c in the text.
  2. Of the three b characters, two remain between those fixed parts. The b* must consume exactly 2.
DSA compiler