You are given a compressed string and a full expanded string. Decide whether the compressed string matches the expanded string.
In the compressed string, a letter stands for itself and a number stands for that many characters of the expanded string that were skipped. For example, "k8s" matches "kubernetes": k matches the first character, 8 covers the 8 middle characters ("ubernete"), and s matches the last character.
Numbers can have several digits, so "a12b" means the letter a, then 12 skipped characters, then the letter b. Return True if the compressed string matches the expanded string and False otherwise.
Function Signature
def is_match(compressed: str, expanded: str) -> bool:
Rules
-
A maximal run of consecutive digits in
compressed
is one decimal number.
"12"
means 12 skipped characters, never 1 and then 2.
-
Numbers are read at face value: leading zeros are allowed (
"05"
skips 5 characters) and
0
skips nothing.
-
The strings match when the compressed string covers every character of
expanded
exactly once, in order. Each letter must equal the character it lines up with, and the letters plus the skipped counts must add up to exactly
len(expanded)
.
Constraints
-
1 <= len(compressed) <= 1000
-
1 <= len(expanded) <= 10^4
-
compressed
consists of lowercase English letters and digits (
0-9
).
-
expanded
consists of lowercase English letters only.
Examples
Input: compressed = "k8s", expanded = "kubernetes"
Output: True
k matches k, 8 skips "ubernete", and s matches the final s. The pattern covers 10 characters in total, which is exactly the length of "kubernetes".
Input: compressed = "i18n", expanded = "internationalization"
Output: True
The two digits form the single number 18. i matches the first character, 18 characters ("nternationalizatio") are skipped, and n matches the last of the 20 characters.
Input: compressed = "k1b5s", expanded = "kubernetes"
Output: False
k matches, 1 skips u, and b matches the b at index 2. Then 5 skips "ernet", so s lines up with the e at index 8 and the match fails. The pattern also covers only 9 characters, one short of 10.