Valid Word Abbreviation

easy string two pointers

Problem

A non-empty string abbr abbreviates a non-empty word. Return whether abbr is a valid abbreviation of word.

Inputword = "internationalization", abbr = "i12iz4n"
Outputtrue
i + 12 + iz + 4 + n perfectly traverses the word.

def valid_word_abbreviation(word, abbr):
    i = j = 0
    while i < len(word) and j < len(abbr):
        if abbr[j].isdigit():
            if abbr[j] == "0": return False
            num = 0
            while j < len(abbr) and abbr[j].isdigit():
                num = num * 10 + int(abbr[j]); j += 1
            i += num
        else:
            if word[i] != abbr[j]: return False
            i += 1; j += 1
    return i == len(word) and j == len(abbr)
function validWordAbbreviation(word, abbr) {
  let i = 0, j = 0;
  while (i < word.length && j < abbr.length) {
    if (abbr[j] >= "0" && abbr[j] <= "9") {
      if (abbr[j] === "0") return false;
      let num = 0;
      while (j < abbr.length && abbr[j] >= "0" && abbr[j] <= "9") { num = num * 10 + Number(abbr[j]); j++; }
      i += num;
    } else {
      if (word[i] !== abbr[j]) return false;
      i++; j++;
    }
  }
  return i === word.length && j === abbr.length;
}
class Solution {
    public boolean validWordAbbreviation(String word, String abbr) {
        int i = 0, j = 0;
        while (i < word.length() && j < abbr.length()) {
            if (Character.isDigit(abbr.charAt(j))) {
                if (abbr.charAt(j) == '0') return false;
                int num = 0;
                while (j < abbr.length() && Character.isDigit(abbr.charAt(j))) num = num * 10 + (abbr.charAt(j++) - '0');
                i += num;
            } else {
                if (word.charAt(i) != abbr.charAt(j)) return false;
                i++; j++;
            }
        }
        return i == word.length() && j == abbr.length();
    }
}
bool validWordAbbreviation(string word, string abbr) {
    int i = 0, j = 0;
    while (i < (int)word.size() && j < (int)abbr.size()) {
        if (isdigit(abbr[j])) {
            if (abbr[j] == '0') return false;
            int num = 0;
            while (j < (int)abbr.size() && isdigit(abbr[j])) num = num * 10 + (abbr[j++] - '0');
            i += num;
        } else {
            if (word[i] != abbr[j]) return false;
            i++; j++;
        }
    }
    return i == (int)word.size() && j == (int)abbr.size();
}
Time: O(|word| + |abbr|) Space: O(1)