My code:
public class Solution {
public boolean validWordAbbreviation(String word, String abbr) {
int wi = 0;
int ai = 0;
int cnt = 0;
while (ai < abbr.length()) {
while (ai < abbr.length() && Character.isDigit(abbr.charAt(ai))) {
if (cnt == 0 && abbr.charAt(ai) == '0') {
return false;
}
cnt = 10 * cnt + abbr.charAt(ai) - '0';
ai++;
}
wi += cnt;
cnt = 0;
if (ai < abbr.length()) {
if (wi >= word.length()) {
return false;
}
else if (word.charAt(wi) != abbr.charAt(ai)) {
return false;
}
else {
wi++;
ai++;
}
}
}
return wi == word.length();
}
}
reference:
https://discuss.leetcode.com/topic/61435/simplest-java-solution-so-far
沒做出來秒赤。
差點(diǎn)做出來“┠唬卡在了一個(gè)問題上。 可能會(huì)出現(xiàn) 015 這樣的數(shù)字神年,這是錯(cuò)的纱耻。
Anyway, Good luck, Richardo! -- 10/21/2016