JAVA Easy Solution


  • 0
    I
        public boolean validWordAbbreviation(String word, String abbr) {
            int idx=0;
            for(int i=0;i<abbr.length();){
                char c = abbr.charAt(i);
                if(c-'0'>=0&&c-'0'<=9){
                    String number="";
                while(i<abbr.length()&&abbr.charAt(i)-'0'>=0&&
                         abbr.charAt(i)-'0'<=9){
                    number+=abbr.charAt(i);
                    i++;
                }
           
                if(number.startsWith("0")) return false;
                idx+=Integer.valueOf(number);
              
                }
                else{
                   
                    if(idx>word.length()-1||abbr.charAt(i)!=word.charAt(idx))
                    return false;
                    i++;
                    idx++;
                }
                
            }
            return word.length()==idx&&word.length()>=abbr.length();
            
        }
         
    }
    

Log in to reply
 

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