Valid Word Abbreviation
Given anon-emptystringsand an abbreviationabbr, return whether the string matches with the given abbreviation.
A string such as"word"contains only the following valid abbreviations:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]Notice that only the above abbreviations are valid abbreviations of the string"word". Any other string is not a valid abbreviation of"word".
Note:
Assumescontains only lowercase letters andabbrcontains only lowercase letters and digits.
Example 1:
Given
s
= "internationalization",
abbr
= "i12iz4n":
Return true.Example 2:
Given
s
= "apple",
abbr
= "a2e":
Return false.Analysis
Easy题,但是有许多实现细节容易出问题。
用
Character.isDigit(t[j])来检测是否为数字数字字符到数字的转换
while嵌套while循环要注意内层循环中的指针不要越界
Solution
Reference
Last updated
Was this helpful?