Given s = "internationalization", abbr = "i12iz4n":

Return true.

Given s = "apple", abbr = "a2e":

Return false.
public class Solution {
    public boolean validWordAbbreviation(String word, String abbr) {
        int len = 0;
        int i = 0, j = 0;
        
        while(i < word.length() && j < abbr.length()){
            if(word.charAt(i) == abbr.charAt(j)){
                i++;
                j++;
                continue;
            }
            if(abbr.charAt(j) <= '0' || abbr.charAt(j) > '9'){
                return false;
            }
            int start = j;
            while(j< abbr.length() && abbr.charAt(j) >= '0' && abbr.charAt(j) <= '9'){
                j++;
            }
            int num = Integer.valueOf(abbr.substring(start, j));
            i += num;
        }
        
        return i == word.length() && j == abbr.length();
    }
}

大米中的大米
12 声望5 粉丝

你的code是面向面试编程的,收集和整理leetcode discussion里个人认为的最优且最符合我个人思维逻辑的解法。