Simple two pointers java solution


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

Log in to reply
 

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