27. Valid Word Abbreviation
easyAsked at MetaCheck if a numeric abbreviation is valid for a word — Meta's search autocomplete and username compression features depend on this two-pointer string matching to validate user-typed abbreviations instantly.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
A string can be abbreviated by replacing any number of non-adjacent substrings with their lengths. Given a string word and an abbreviation abbr, return whether the abbreviation matches the word. A leading zero in a number is not valid.
Constraints
1 <= word.length <= 20word consists of lowercase English letters only1 <= abbr.length <= 10abbr consists of lowercase English letters and digits
Examples
Example 1
word = "internationalization", abbr = "i12iz4n"trueExplanation: i + 12 chars + iz + 4 chars + n matches internationalization.
Example 2
word = "apple", abbr = "a3e"falseExplanation: a + 3 chars + e would need 5 chars total but skips the 'p' incorrectly.
Approaches
1. Brute force (expand abbreviation)
Expand the abbreviation into a concrete string and compare to word. Requires parsing digits and substituting character runs — error-prone. Two-pointer is cleaner.
- Time
- O(|word|+|abbr|)
- Space
- O(|word|)
function validWordAbbreviation(word, abbr) {
let expanded = '';
let i = 0;
while (i < abbr.length) {
if (abbr[i] >= '0' && abbr[i] <= '9') {
if (abbr[i] === '0') return false;
let num = 0;
while (i < abbr.length && abbr[i] >= '0' && abbr[i] <= '9') num = num*10 + +abbr[i++];
expanded += '_'.repeat(num);
} else {
expanded += abbr[i++];
}
}
if (expanded.length !== word.length) return false;
for (let j = 0; j < word.length; j++) {
if (expanded[j] !== '_' && expanded[j] !== word[j]) return false;
}
return true;
}Tradeoff:
2. Two pointers (optimal)
Walk both word and abbr with two pointers. When a digit is seen in abbr, parse the full number (reject leading zeros), advance the word pointer by that count, then continue matching characters.
- Time
- O(max(|word|,|abbr|))
- Space
- O(1)
function validWordAbbreviation(word, abbr) {
let i = 0, j = 0;
while (i < word.length && j < abbr.length) {
if (abbr[j] >= '1' && abbr[j] <= '9') {
let num = 0;
while (j < abbr.length && abbr[j] >= '0' && abbr[j] <= '9') num = num*10 + parseInt(abbr[j++]);
i += num;
} else if (abbr[j] === '0') {
return false; // leading zero
} else {
if (word[i] !== abbr[j]) return false;
i++; j++;
}
}
return i === word.length && j === abbr.length;
}Tradeoff:
Meta-specific tips
Meta considers this a signal problem for attention to edge cases: leading zeros ('01' is invalid), a number that skips past the end of word, and an abbreviation that ends mid-number. Enumerate edge cases out loud before coding — Meta interviewers reward systematic thinking even on easy problems.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Valid Word Abbreviation and other Meta interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →