336. Valid Word Abbreviation
A word can be shortened by picking chunks of consecutive letters — each chunk non-empty, and no two chunks touching each other — and replacing every chunk with the count of letters it removed, written in decimal. From substitution you could make s10n (one 10-letter chunk) or su3itu2on (a 3-letter and a 2-letter chunk). Replacing nothing at all is allowed too, so a word is an abbreviation of itself.
A count is never written with a leading zero. That outlaws 0 (an empty chunk) entirely and strings like s010n, though a 0 inside a longer number such as 10 is fine.
You receive a lowercase word word and a candidate string abbr (lowercase letters and digits). Return true if abbr can be produced from word by these rules, and false otherwise.
Example 1:
Input: word = "internationalization", abbr = "i12iz4n"
Output: true
Explanation: i + skip 12 letters (nternationa) + iz + skip 4 letters (atio) + n spells out all 20 characters of the word exactly: 1 + 12 + 2 + 4 + 1 = 20.
Example 2:
Input: word = "apple", abbr = "a2e"
Output: false
Explanation: a + skip 2 (pp) + e accounts for only 4 characters, but after skipping "pp" the next letter of the word is 'l', not 'e'. No choice of chunks turns apple into a2e.
Constraints:
- 1 ≤ word.length ≤ 5000
- word consists of lowercase English letters
- 1 ≤ abbr.length ≤ 5000
- abbr consists of lowercase English letters and digits
Hints:
Walk both strings at once with two indices: i into word, j into abbr. A letter in abbr must literally match word[i]; a digit starts a number that tells you how far to jump i forward.
Parse the whole number before jumping (e.g. "12" is one jump of twelve, not 1 then 2), and reject a number the moment it starts with '0'. At the end, both pointers must land exactly on the ends of their strings.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: word = "internationalization", abbr = "i12iz4n"
Expected output: true