Valid Word Abbreviation
The problem
A string can be abbreviated by replacing any number of non-adjacent, non-empty substrings with their respective lengths. The lengths must not contain leading zeros.
For instance, a word like "celebration" could be abbreviated as (but is not limited to):
- "c8n" ("c elebratio n")
- "ce3r3on" ("ce leb r ati on")
- "10" ("celebration")
- "ce1e2r2o1" ("ce l e b r a t io n")
- "celebration" (no substrings replaced)
The following are invalid abbreviations:
- "c99n" ("c eleb r atio n", since the replaced substrings are adjacent)
- "c012n" (contains leading zeros)
- "c0elebration" (replaces an empty substring)
Given a string word and an abbreviation abbr, determine whether the string correctly matches the given abbreviation.
A substring is a contiguous, non-empty sequence of characters within a string.
Input: word = "development", abbr = "d9t" Output: true Explanation: The word "development" can be abbreviated as "d9t" ("d evelopmen t").
Input: word = "programming", abbr = "p8g" Output: true Explanation: The word "programming" can be abbreviated as "p8g" ("p rogrammin g").
Input: word = "keyboard", abbr = "k2y2d"
- 1 <= word.length <= 20
- word consists of only lowercase English letters.
- 1 <= abbr.length <= 10
- abbr consists of lowercase English letters and digits.
- All integers in abbr will fit in a 32-bit integer.
cpp
class Solution {
public:
bool validWordAbbreviation(string word, string abbr) {
// Your code goes here
}
};java
class Solution {
public boolean validWordAbbreviation(String word, String abbr) {
// Your code goes here
}
}python
class Solution:
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
# Your code goes herejavascript
class Solution {
validWordAbbreviation(word, abbr) {
// Your code goes here
}
}csharp
public class Solution
{
public bool ValidWordAbbreviation(string word, string abbr)
{
// Your code goes here
}
}go
func validWordAbbreviation(word string, abbr string) bool {
//your code goes here
}Stuck? Show a way to structure it+
- 01Walk word and abbreviation with separate indices.
- 02Match letters directly.
- 03Parse consecutive digits into one positive skip count.
- 04Reject leading zeros, overruns, and mismatched endings.
Reference answer
Then expect these follow-ups
How would you return the first mismatch location?
Tests: diagnostics
How would Unicode code points affect indexing?
Tests: representation
Hiring now
Software engineer roles are open right now
landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Senior Software Engineer -.NET
Tyler Technologies · Annapolis, Maryland

Software Development Engineer II
SaaS Labs · Bengaluru, Karnataka, India

Advanced Software Engineer - Product Architect (Legal, Safety, and Risk)
Metropolitan Transportation Authority · New York, NY
Next question