Concise Java Solution


  • 0

    The basic approach

    public boolean validWordAbbreviation(String word, String abbr) {
            int index=0;
            int num=0;
            int len=word.length();
            for(char x:abbr.toCharArray()){
                if(x>='0'&&x<='9'){
                    if(num==0&&x=='0') return false;
                    num*=10;
                    num+=(x-'0');
                }else{
                    index+=num;
                    num=0;
                    if(index>=len || word.charAt(index++)!=x) return false;
                }
            }
            index+=num;
            return index==word.length();
        }
    

Log in to reply
 

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