Short and easy to understand Java Solution


  • 36
    L
        public boolean validWordAbbreviation(String word, String abbr) {
            int i = 0, j = 0;
            while (i < word.length() && j < abbr.length()) {
                if (word.charAt(i) == abbr.charAt(j)) {
                    ++i;++j;
                    continue;
                }
                if (abbr.charAt(j) <= '0' || abbr.charAt(j) > '9') {
                    return false;
                }
                int start = j;
                while (j < abbr.length() && abbr.charAt(j) >= '0' && abbr.charAt(j) <= '9') {
                    ++j;
                }
                int num = Integer.valueOf(abbr.substring(start, j));
                i += num;
            }
            return i == word.length() && j == abbr.length();
        }
    
    

  • 0
    J

    Your code would give wrong answer if the input test case is: word = "internationalization", abbr = "i12iz4n"
    Since the '12' would be separated as single number '1' and '2'. You need to add a if condition before the while condition, after the "int start = j;"
    if(j < abbr.length() && abbr.charAt(j) > '0' && abbr.charAt(j) <= '9') ++j;
    In this way, '12' would be converted to the integer number 12.


  • 0
    I
    This post is deleted!

  • 1

    similar idea here C#

        public bool ValidWordAbbreviation(string word, string abbr) 
        {
            int w = 0;
            int a = 0;
            int num = 0;
            while (w < word.Length && a < abbr.Length)
            {
                if (abbr[a] >= 'a' && abbr[a] <= 'z') 
                {
                    // letter
                    w += num;
                    num = 0;
                    if (w >= word.Length || abbr[a] != word[w]) return false;
                    else 
                    {
                        a++; 
                        w++;
                    }
                }
                else
                {
                    // number
                    num = (10 * num) + (abbr[a] - '0');
                    if (num == 0) return false;
                    a++;
                }
            }
            
            return a == abbr.Length && w + num == word.Length;
        }
    

  • 0
    P
    This post is deleted!

  • 7
    S

    Similar idea; Iterate over the characters of abbr and skip number of characters of word. Then compare ith chracter of word with jth character of abbr.

    public boolean validWordAbbreviation(String word, String abbr) {
            int number = 0;
            int i = 0, j = 0;
            while (i < word.length() && j < abbr.length()) {
                if (Character.isDigit(abbr.charAt(j))) {
                    number = number * 10 + abbr.charAt(j) - '0';
                    if (number == 0) return false;
                    j++;
                } else {
                    i += number;
                    if (i >= word.length() || word.charAt(i) != abbr.charAt(j)) return false;
                    number = 0;
                    i++; j++;
                }
            }
            i += number;
            return i == word.length() && j == abbr.length();
        }
    

  • 0
    Y

    string concat cost more, compact version similar to the idea from above:

    public class Solution {
        public static boolean validWordAbbreviation(String word, String abbr){
            int s1 = 0, s2 = 0;
            while(s2<abbr.length()){
                int margin = 0;//record the number
                while(s2<abbr.length() && Character.isDigit(abbr.charAt(s2))){
                    char cur = abbr.charAt(s2++);
                    if(margin==0 && cur=='0') return false;
                    margin = margin*10 + cur - '0';
                }
                s1 += margin;
                //if the digit is the last part of the abbr
                if(s2==abbr.length()) return s1==word.length();
                //word jumped too much || character not matched
                if(s1>=word.length() || word.charAt(s1++)!=abbr.charAt(s2++)) return false;
            }
            return s1==word.length();//check if there're characters left in word
        }
    }
    

  • 0

    @jdrogin
    Your code seems faster in production! great code and perspective. Always learn a lot from your solution.


Log in to reply
 

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