Valid Word Abbreviation - Problem

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")
  • "su3i1u2on" ("su bst i t u ti on")
  • "substitution" (no substrings replaced)

The following are not valid abbreviations:

  • "s55n" (the replaced substrings are adjacent)
  • "s010n" (has leading zeros)
  • "s0ubstitution" (replaces an empty substring)

Given a string word and an abbreviation abbr, return true if the string matches the given abbreviation, false otherwise.

Input & Output

Example 1 — Valid Abbreviation
$ Input: word = "internationalization", abbr = "i12iz4n"
Output: true
💡 Note: The abbreviation "i12iz4n" represents "i" + 12 characters + "iz" + 4 characters + "n" = "internationalization"
Example 2 — Invalid Leading Zero
$ Input: word = "apple", abbr = "a2e"
Output: false
💡 Note: The abbreviation "a2e" would be "a" + 2 characters + "e" = "apple" but this doesn't match because we need "a" + "pp" + "e"
Example 3 — Leading Zero Invalid
$ Input: word = "substitution", abbr = "s010n"
Output: false
💡 Note: The abbreviation contains "010" which has leading zeros, making it invalid

Constraints

  • 1 ≤ word.length ≤ 20
  • 1 ≤ abbr.length ≤ 10
  • word consists of only lowercase English letters
  • abbr consists of lowercase English letters and digits

Visualization

Tap to expand
Valid Word Abbreviation INPUT word: internationalization Character indices: i 0 n 1 t 2 ... i 13 z 14 ... n 19 abbr: i12iz4n Abbreviation breakdown: i 12 i z 4 n Green = char, Orange = skip count word length: 20 abbr length: 7 ALGORITHM STEPS 1 Initialize Pointers i=0 (word), j=0 (abbr) 2 Traverse Both If digit: parse full number If char: match with word[i] 3 Skip Characters Number skips that many chars i += parsed_number 4 Verify End Both reach end = valid Execution Trace: abbr[j] action i j 'i' match i 0-->1 0-->1 '12' skip 12 1-->13 1-->3 'i' match i 13-->14 3-->4 'z' match z 14-->15 4-->5 '4' skip 4 15-->19 5-->6 'n' match n 19-->20 6-->7 i=20, j=7 (both at end!) FINAL RESULT Mapping Visualization i nternationa (12 chars) l i z atio n i 12 i z 4 n Output: true Verification: [OK] i matches word[0] [OK] 12 skips to index 13 [OK] i,z match word[13,14] [OK] 4 skips to index 19 [OK] n matches word[19] Key Insight: Two-pointer technique: traverse word and abbreviation simultaneously. When a digit is found, parse the complete number (could be multi-digit like "12") and skip that many characters in word. Reject if number has leading zero. Valid only if both pointers reach the end simultaneously. TutorialsPoint - Valid Word Abbreviation | Optimal Two-Pointer Approach - O(n) Time, O(1) Space
Asked in
Google 45 Facebook 32 Microsoft 28
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen