Valid Word Abbreviation
Try to solve the Valid Word Abbreviation problem.
We'll cover the following
Statement
Given a string, word
, and abbreviation, abbr
, return TRUE if the abbreviation matches the given string. Otherwise, return FALSE. An abbreviation can replace any non-adjacent, non-empty substrings of the original word with their lengths. Replacement lengths must not contain leading zeros.
A certain word, "calendar"
, can be abbreviated as follows:
"cal3ar"
("cal +
end
[length = 3] + ar"
skipping three characters"end"
from the word"calendar"
still matches the provided abbreviation)"c6r"
("c +
alenda
[length = 6] + r"
skipping six characters"alenda"
from the word"calendar"
still matches the provided abbreviation)
The word "internationalization"
can also be abbreviated as "i18n"
(the abbreviation comes from skipping 18 characters in "internationalization"
leaving the first and last letters "i"
and "n"
).
The following are not valid abbreviations:
"c06r"
(Has leading zeroes)"cale0ndar"
(Replaces an empty string)"c24r"
("c
al
enda
r"
the replaced are adjacent)substrings A substring is a contiguous non-empty sequence of characters within a string.
Constraints:
word.length
The
word
consists of only lowercase English letters.abbr.length
abbr
consists of lowercase English letters and digits.All the integers in
abbr
will fit in a–bit integer.
Examples
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.