20ms


  • 0
    Q
    public class Solution {
        public boolean validWordAbbreviation(String word, String abbr) {
            char[] wChars = word.toCharArray();
            char[] aChars = abbr.toCharArray();
            int wIndex = 0, aIndex = 0;
            while (wIndex < wChars.length && aIndex < aChars.length) {
                if (aChars[aIndex] >= 'a' && aChars[aIndex] <= 'z') {
                    if (aChars[aIndex] != wChars[wIndex]) {
                        return false;
                    }
                    aIndex++;
                    wIndex++;
                } else if (aChars[aIndex] >= '1' && aChars[aIndex] <= '9') {
                    int adv = aChars[aIndex]-'0';
                    aIndex++;
                    while (aIndex < aChars.length && aChars[aIndex] >= '0' && aChars[aIndex] <= '9') {
                        adv *= 10;
                        adv += (aChars[aIndex++]-'0');
                    }
                    wIndex += adv;
                } else {
                    return false;
                }
            }
            return wIndex == wChars.length && aIndex == aChars.length;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.