DSA problem
Valid Word Abbreviation
A string can be abbreviated by replacing any number of non-adjacent, non-empty substrings with their lengths. The lengths should not have leading zeros.
For example, a string such as "substitution" could be abbreviated as (but not limited to):
"s10n"("s ubstitutio n")"sub4u4"("sub stit u tion")"12"("substitution")
Given a string word and an abbreviation abbr, return true if the string matches the given abbreviation, and false otherwise.
Examples
Example 1
Input:
word = "internationalization", abbr = "i12iz4n"Output:
trueExplanation: The word "internationalization" can be abbreviated as "i12iz4n" ("i nternational iz atio n").
Example 2
Input:
word = "apple", abbr = "a2e"Output:
falseExplanation: "a2e" expands to "a__e" which has length 4, while "apple" has length 5.
Example 3
Input:
word = "internationalization", abbr = "i5a11o1"Output:
trueExplanation: The abbreviation expands to match: "i" + skip 5 + "a" + skip 11 + "o" + skip 1.
Constraints
- 1 <= word.length <= 20
- word consists of only lowercase English letters.
- 1 <= abbr.length <= 10
- abbr consists of lowercase English letters and digits.
- All the integers in abbr will fit in a 32-bit integer.
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.