Search
⌘K

Leetcode 408. Valid Word Abbreviation

Check whether a given abbreviation (letters mixed with digits that denote how many characters to skip) correctly represents a target word. The core challenge is parsing multi-digit numbers (no leading zeros) and using a two-pointer traversal to advance through the word accordingly while matching letters.

Asked at:

Meta


DESCRIPTION

Check whether a given abbreviation (letters mixed with digits that denote how many characters to skip) correctly represents a target word. The core challenge is parsing multi-digit numbers (no leading zeros) and using a two-pointer traversal to advance through the word accordingly while matching letters.

Input:

word = "internationalization", abbr = "i12iz4n"

Output:

true


Explanation: The abbreviation correctly represents the word: 'i' + (skip 12: 'nternational') + 'iz' + (skip 4: 'atio') + 'n'

Constraints:

  • 1 <= word.length <= 20
  • 1 <= abbr.length <= 10
  • word consists of only lowercase English letters
  • abbr consists of lowercase English letters and digits
  • All numbers in abbr will fit in a 32-bit integer
  • Numbers in abbr have no leading zeros

Understanding the Problem

Let's understand what we're being asked to do. We have a target word like 'internationalization' and an abbreviation like 'i12iz4n'.

The abbreviation mixes letters with numbers. Each number tells us how many characters to skip. Let's trace through: 'i' matches the first letter, '12' means skip 12 characters ('nternational'), 'iz' matches the next two letters, '4' means skip 4 characters ('atio'), and 'n' matches the last letter.

Important constraint: Numbers in the abbreviation have no leading zeros. So '01' would be invalid, but '10' is valid.

Edge cases to consider: What if the abbreviation is just the word itself with no numbers? What if it's all numbers? What if the numbers add up to more characters than the word has? What if we encounter '0' (which would mean skip zero characters - is this valid)?

Building Intuition

We need to process the abbreviation character by character, but when we encounter a digit, we must collect all consecutive digits to form the complete number.

For example, in 'i12iz4n', when we see '1', we can't just skip 1 character - we need to keep reading to get '12' (skip 12 characters).

This is a parsing challenge combined with a two-pointer traversal. One pointer moves through the abbreviation (handling both letters and multi-digit numbers), while another pointer moves through the target word.

If we parse numbers incorrectly (treating '12' as '1' then '2'), we'll advance through the word incorrectly and get wrong results.

Think of reading instructions for a treasure map: 'Walk 3 steps north, then 15 steps east, then 2 steps south.'

You can't read '15' as '1 step, then 5 steps' - you need to read the full number before moving. Similarly, when parsing 'i12iz4n', you must collect all digits of '12' before advancing 12 positions in the word.

The key is: when you see a digit, keep collecting until you hit a non-digit, then use that complete number to advance your position.

Common Mistakes

Optimal Solution

Use two pointers: one for the word and one for the abbreviation. Iterate through the abbreviation character by character. When encountering a letter, check if it matches the current character in the word and advance both pointers. When encountering a digit, parse the complete multi-digit number (collecting all consecutive digits), then advance the word pointer by that amount. Return true if both pointers reach the end simultaneously, false otherwise.

|
string
|
string
Visualization
def valid_word_abbreviation(word, abbr):
"""
Check if abbreviation matches word using two-pointer traversal.
Parse multi-digit numbers and advance word pointer accordingly.
"""
word_ptr = 0
abbr_ptr = 0
while abbr_ptr < len(abbr):
if abbr[abbr_ptr].isdigit():
if abbr[abbr_ptr] == '0':
return False
num = 0
while abbr_ptr < len(abbr) and abbr[abbr_ptr].isdigit():
num = num * 10 + int(abbr[abbr_ptr])
abbr_ptr += 1
word_ptr += num
else:
if word_ptr >= len(word) or word[word_ptr] != abbr[abbr_ptr]:
return False
word_ptr += 1
abbr_ptr += 1
return word_ptr == len(word)
internationalization012345678910111213141516171819

Start validating abbreviation against word

0 / 28

Test Your Knowledge

Login to take the complexity quiz and track your progress

Complexity Analysis

Time Complexity: O(n + m) We traverse the word once (length n) and the abbreviation once (length m), processing each character exactly once

Space Complexity: O(1) We only use two pointers and a variable to accumulate multi-digit numbers, requiring constant extra space

What We've Learned

  • Two-pointer string-number parsing: When dealing with strings that mix characters and numeric values, use two pointers (one for the word, one for the abbreviation) to independently traverse and synchronize based on parsed values - this allows flexible advancement without complex indexing.
  • Multi-digit number accumulation: Parse consecutive digits by building the number incrementally (num = num * 10 + digit) rather than substring extraction - this handles variable-length numbers efficiently in a single pass without additional string operations.
  • Leading zero validation: Always check if a digit sequence starts with '0' when parsing numbers from strings - leading zeros indicate invalid input in most numeric contexts, and this check must happen before accumulation begins to catch malformed data early.
  • Boundary condition checking: After processing all abbreviation characters, verify that the word pointer has reached exactly the end of the target string - partial matches or overshooting both indicate invalid abbreviations, a common oversight in string matching problems.
  • Character-by-character vs skip-ahead logic: When an abbreviation contains both literal characters (must match exactly) and numbers (skip ahead), structure your code with clear branching - if it's a digit, accumulate and skip; if it's a letter, compare and advance one step.
  • String validation pattern: This two-pointer technique with conditional advancement extends to any problem involving compressed representations, run-length encoding validation, or pattern matching where some elements represent literal values and others represent counts or operations.

Related Concepts and Problems to Practice

Overview
Lesson
Two Pointers

This lesson introduces the two-pointer technique which is the core pattern used in Valid Word Abbreviation. Understanding two-pointer traversal fundamentals will help students recognize when and how to use independent pointers to traverse different sequences at different rates.

Decode String

medium

Stack

Like Valid Word Abbreviation, this problem requires parsing strings with embedded numbers and handling multi-digit number extraction. Both problems involve character-by-character traversal while building up numeric values and applying them to transform or validate strings.

Decode Ways

medium

Dynamic Programming

This problem shares the pattern of parsing digit sequences within strings and making decisions based on multi-digit numbers. Both require careful handling of leading zeros and converting consecutive digits into meaningful numeric values during string traversal.

Test Your Understanding

Why is array the right data structure for this problem?

1

array provides the optimal access pattern

2

It's the only data structure that works

3

It's the easiest to implement

4

It uses the least memory

Select an answer to see the explanation

Question Timeline

See when this question was last asked and where, including any notes left by other candidates.

Mid February, 2026

Meta

Mid-level

Early February, 2026

Meta

Staff

This was one of the questions asked to me in the Technical Screening round of my E6 loop. The problem was asked in the exact same way as it is on Leetcode.

Late December, 2025

Meta

Senior

Comments

Your account is free and you can post anonymously if you choose.