Fast java solution


  • 0
    M
    public boolean validWordAbbreviation(String word, String abbr) {
        int i = 0, n = 0, k = word.length();
        for(char c : abbr.toCharArray()) {
            if(c >= 'a' && c <= 'z') {
                i += n;
                n = 0;
                if(i >= k || c != word.charAt(i))
                    return false;
                i++;    
            } else if(c == '0' && n == 0){
                return false;
            } else {    
                n = n * 10 + c - '0';
            }
        }
        return (i + n) == k;
    }

Log in to reply
 

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