my easy java solution


  • 0
    
        public boolean validWordAbbreviation(String word, String abbr) {
            if(word.length() < abbr.length()){
                return false;
            }
            int index_word = 0;
            int index_abbr = 0;
            while(index_word < word.length() && index_abbr < abbr.length()){
                if(word.charAt(index_word) == abbr.charAt(index_abbr)){
                    index_word++;
                    index_abbr++;
                }else if(abbr.charAt(index_abbr) >= '1' && abbr.charAt(index_abbr) <= '9'){
                    int start = index_abbr;
                    while(index_abbr < abbr.length() && abbr.charAt(index_abbr) >= '0' && abbr.charAt(index_abbr) <= '9'){
                        index_abbr++;
                    }
                    index_word += Integer.valueOf(abbr.substring(start, index_abbr));
                }else{
                    return false;
                }
             }
            return index_word == word.length() && index_abbr == abbr.length();
        }
    
    

Log in to reply
 

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