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
Understanding the Visualization
1
Input
word="internationalization", abbr="i12iz4n"
2
Process
Match characters and parse numbers to skip sections
3
Output
Return true if abbreviation is valid for the word
Key Takeaway
🎯 Key Insight: Numbers in abbreviations tell us exactly how many characters to skip in the original word
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code