AlgoInsight

DSA problem

Valid Word Abbreviation

EasyTwo PointersPattern: Two Pointerstop600FacebookGoogleAmazon

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):

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: true
Explanation: The word "internationalization" can be abbreviated as "i12iz4n" ("i nternational iz atio n").
Example 2
Input: word = "apple", abbr = "a2e"
Output: false
Explanation: "a2e" expands to "a__e" which has length 4, while "apple" has length 5.
Example 3
Input: word = "internationalization", abbr = "i5a11o1"
Output: true
Explanation: The abbreviation expands to match: "i" + skip 5 + "a" + skip 11 + "o" + skip 1.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

two-pointersstring

Browse all DSA problems