share my java solution!


  • 0
    C
    public class Solution {
        public boolean validWordAbbreviation(String word, String abbr) {
            char[] wordarray=word.toCharArray();
            char[] array=abbr.toCharArray();
            
            int i=0;
            int j=0;
            while(i<wordarray.length&&j<array.length){
                int k=j;
                if(array[j]<'0'||array[j]>'9'){
                    if(wordarray[i]!=array[j]){
                        return false;
                    }
                    i++;
                    j++;
                }else{
                    if(array[j]=='0') return false;
                    
                    while(j<array.length&&array[j]>='0'&&array[j]<='9'){
                        j++;
                    }
                    int num=Integer.valueOf(abbr.substring(k,j));
                    i+=num;
                }
            }
            
            return i==wordarray.length&&j==array.length;
        }
    }
    
    

Log in to reply
 

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