码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 409: Valid Word Abbreviation

时间:2017-10-08 16:13:18      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:else   star   cas   bsp   iat   solution   col   har   ++   

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

标签:else   star   cas   bsp   iat   solution   col   har   ++   

原文地址:http://www.cnblogs.com/shuashuashua/p/7637558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!