We're given two strings: the original word and an abbr that may contain a mix of letters and numbers. Each number in the abbreviation represents how many characters from the original word were replaced. Our job is to verify that the abbreviation correctly describes the original word.
Three rules govern what counts as a match:
12 means skip 12 characters, not 1 then 2.01 is invalid, and so is 0 by itself, since a number replaces a non-empty substring and zero characters is empty.int never overflows. We do not need a 64-bit type for the running number.abbr mixes letters and digits, so the parser has to branch on character type and consume a full run of digits at once rather than one digit at a time.Expand the abbreviation back into a full-length string, then compare it with the word.
Each letter in the abbreviation maps to one position in the expanded string. Each number maps to that many placeholder positions, which we represent with a wildcard character that matches anything. Once the abbreviation is fully expanded, two checks decide the answer: the expanded length must equal the word length, and every non-wildcard position must match the word.
j for the abbreviation and a list to build the expanded string.The expanded array is the only reason this uses O(n) space. The next approach removes it by advancing a pointer through the word directly instead of materializing wildcards.
The expansion in Approach 1 is unnecessary because the comparison can happen as we parse. Walk both strings with two pointers: i in the word, j in the abbreviation. A letter at abbr[j] consumes one word character; a number at abbr[j] consumes that many. Decoding and matching happen in the same pass, with no intermediate string.
The logic per step:
abbr[j] is a letter, compare it with word[i]. If they match, advance both pointers. If not, return false.abbr[j] is a digit, parse the full number (rejecting a leading zero), then advance i by that amount.When the loop ends, both pointers must sit exactly at the end of their strings.
The loop stops as soon as either pointer reaches the end of its string, so it can exit with the other pointer still short. Two failing cases survive the loop body and are caught only by the final check.
If j reaches abbr.length but i < word.length, the abbreviation decoded into fewer characters than the word holds, so trailing word characters went unmatched. If a number near the end of abbr pushes i past word.length while digits remain in abbr, the abbreviation claims more characters than the word has. Requiring i == word.length && j == abbr.length rejects both.
i = 0 for the word, j = 0 for the abbreviation.abbr[j] is a digit:'0', return false (leading zero).i by that number.abbr[j] is a letter:word[i] != abbr[j], return false.i and j.i == word.length and j == abbr.length.i only moves forward) and the abbreviation at most once (pointer j only moves forward). Each character in both strings is processed exactly once.i, j, num) regardless of input size. No extra data structures needed.