376. Valid Word Abbreviation

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):

  • "c9n" ("c elebratio n")
  • "ce3r3on" ("ce leb r ati on")
  • "11" ("celebration")
  • "ce1e1r3o1" ("ce l e b r ati o 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.

Example 1:

Input: word = "development", abbr = "d9t"

Output: true

Explanation: The word "development" can be abbreviated as "d9t" ("d evelopmen t").

Example 2:

Input: word = "programming", abbr = "p8g"

Output: true

Explanation: The word "programming" can be abbreviated as "p8g" ("p rogrammin g").

Now Your Turn!

Pick the correct output for the given input

Input: word = "keyboard", abbr = "k2y2d"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 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.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
bool validWordAbbreviation(string word, string abbr) {
// Your code goes here
}
};
Test Case

Input:

Abbr
Word