Encoded String Match
Problem statement
Return whether pattern matches the entire text. Matching is case-sensitive. Parse the pattern from left to right using these rules:
- A maximal unescaped run of decimal digits skips exactly that many arbitrary text characters. Thus
d3dogmatchesdatadog, anda11ymatchesaccessibility. {lo,hi}skips any number of arbitrary characters fromlothroughhi, inclusive. Each occurrence chooses its count independently.- A backslash
\or caret^escapes the next single character, which must then match literally. The escape marker consumes no text. This exercise gives both markers the same literal-escape behavior. - Every other character matches itself. An unescaped
{begins a range; an unescaped}appears only as its closing delimiter. Escape a digit or reserved character when it should be literal.
A skip of zero consumes nothing. Escapes always have a following character. Empty text and empty pattern match each other. A match must consume all text characters and all pattern tokens; matching only a prefix is insufficient.
Function
encodedMatch(text: String, pattern: String) → booleanExamples
Example 1
text = "datadog"pattern = "d3dog"return = trueThe first d matches, the skip consumes ata, and the remaining literal dog matches. All seven text characters are consumed.
Example 2
text = "accessibility"pattern = "a11y"return = trueThe first a and last y match literally. The eleven characters between them are consumed by the single number 11.
Example 3
text = "d3dog"pattern = "d\\3dog"return = trueThe backslash escapes 3, so it must match the literal digit in the text. The other four characters match literally; no skip occurs.
Example 4
text = "datadog"pattern = "d\\3dog"return = falseAfter the first d, the escaped 3 requires a literal digit. The next text character is a, so the match fails.
Example 5
text = "datadog"pattern = "d{1,3}dog"return = trueChoose the allowed skip length 3 to consume ata. The literal prefix d and suffix dog then consume the whole text.
Constraints
0 <= text.length, pattern.length <= 200- Both inputs contain only printable ASCII characters (codes 32 through 126).
- The pattern is valid under the grammar above. Every numeric value is between 0 and 1000000000 inclusive, written as
0or without a leading zero. Each range haslo <= hi.