首页 > 其他 > 详细

408. Valid Word Abbreviation --字符串处理

时间:2018-11-12 14:19:29      阅读:154      评论:0      收藏:0      [点我收藏+]
Given s = "internationalization", abbr = "i12iz4n":

Return true.

abbr 里数字代表相应的字符数,问字符串是否相等

虽然是一个easy 的题但却有两个坑:
1. abbr 结尾的地方是数字 例如:
s= "internationalization" abbr= "i5a11o1" , 因此 return时得加上cout 来判断 index + Integer.valueOf(count)
2.字符中 有 0, 例如 s= "a" abbr= "01" 因此只要出现一个不是其他数字后面的0 都是非法的, 比如 01 非法, 而10 合法。加上这个判断
  if(count.equals("0") && c==‘0‘) return false;
 
class Solution {
    public boolean validWordAbbreviation(String word, String abbr) {
        int index = 0;
        String count = "0";
        for(int i=0; i<abbr.length(); i++){
            char c = abbr.charAt(i);  
            if(Character.isLetter(c) ) {
                index = index + Integer.valueOf(count);
                if(index >= word.length() || c != word.charAt(index) ) return false;
                count = "0";
                index++;
            }  
            else {
                if(count.equals("0") && c==‘0‘) return false;
                count += c;
               
            }
        }
    
        return index + Integer.valueOf(count) == word.length();
    }
}

 

408. Valid Word Abbreviation --字符串处理

原文:https://www.cnblogs.com/keepAC/p/9945943.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!