Java solution


  • 0
    Q
    public class Solution {
        public boolean validWordAbbreviation(String word, String abbr) {
            for(int i = 0, wordI = 0; i < abbr.length(); i++, wordI++){
                String curNum = "";
                while(i < abbr.length() && Character.isDigit(abbr.charAt(i))){
                    curNum += abbr.charAt(i);
                    i++;
                }
                if(curNum.startsWith("0")) return false;
                int num = curNum == ""? 0: Integer.parseInt(curNum) ;
                wordI += num;
                if(wordI == word.length() && i == abbr.length()) return true;
                else if(wordI >= word.length() || i >= abbr.length()) return false;
     
                if(word.charAt(wordI) != abbr.charAt(i)){
                    return false;
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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