LeetCode 409: Valid Word Abbreviation

Posted keepshuatishuati

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 409: Valid Word Abbreviation相关的知识,希望对你有一定的参考价值。

Note:

1. 0 could be 10 but not valid for 01. Check edge cases.

2. No number shoud be there as num reduced.

class Solution {
    public boolean validWordAbbreviation(String word, String abbr) {
        if (abbr.length() > word.length()) return false;
        int runner = 0, i = 0, star = 0;
        for (; i < word.length(); i++) {
            if (star == 0) {
                while (runner < abbr.length() && abbr.charAt(runner) >= ‘0‘ && abbr.charAt(runner) <= ‘9‘) {
                    if (star == 0 && abbr.charAt(runner) == ‘0‘) return false;
                    star = star * 10 + (int)(abbr.charAt(runner++) - ‘0‘);
                }
            }
            
            if (star > 0) star--;
            else if (runner < abbr.length() && word.charAt(i) == abbr.charAt(runner)) runner++;
            else return false;
        }
        
        return i == word.length() && runner == abbr.length() && star == 0;
    }
}

 

以上是关于LeetCode 409: Valid Word Abbreviation的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Valid Word Abbreviation

Leetcode408. Valid Word Abbreviation

Leetcode: Valid Word Abbreviation

Valid Word Abbreviation Leetcode

LeetCode: Valid Word Square

leetcode408 - Valid Word Abbreviation - easy