Valid Word Abbreviation

https://www.lintcode.com/zh-cn/problem/valid-word-abbreviation/

public class Solution {
    /**
     * @param word: a non-empty string
     * @param abbr: an abbreviation
     * @return: true if string matches with the given abbr or false
     */
    public boolean validWordAbbreviation(String word, String abbr) {
        // write your code here
        char[] chars = word.toCharArray();
        char[] chars1 = abbr.toCharArray();
        int index1 = 0;
        int index2 = 0;
        while (true) {
            if (index1 == chars.length) {
                return true;
            }
            if (index1 > chars.length) {
                return false;
            }
            char c = chars1[index2];
            if (Character.isDigit(c)) {
                int temp = index2;
                while (true) {
                    if (index2 < abbr.length() && Character.isDigit(chars1[index2])) {
                        index2++;
                    } else {
                        break;
                    }
                }
                String substring = abbr.substring(temp, index2);
                if (substring.startsWith("0")) {
                    return false;
                }
                int i = Integer.parseInt(substring);
                index1 += i;
            } else {
                if (chars[index1] == chars1[index2]) {
                    index1++;
                    index2++;
                } else {
                    return false;
                }
            }
        }
    }
}

你可能感兴趣的:(Valid Word Abbreviation)