zoukankan      html  css  js  c++  java
  • 408. Valid Word Abbreviation

    感冒之后
    image
    睡了2天觉
    image
    现在痊愈了
    image

    重启刷题进程。。

    Google的题,E难度。。
    比较的方法很多,应该是为后面的题铺垫的。

    题不难,做对不容易,edge cases很多,修修改改好多次,写完发现是一坨。

    奉劝大家尽量多想edge cases,想不出来了再提交,别像我似的赶着投胎就提交了。

    想完了先自己试试下面这些test cases...

    "internationalization"
    "i12iz4n"
    "word"
    "1or1"
    "apple"
    "a2e"
    "a"
    "2"
    "b"
    "1"
    "a"
    "01"
    "hi"
    "hi1"
    "hi"
    "2i"
    
    public class Solution 
    {
        public boolean validWordAbbreviation(String word, String abbr) 
        {
            if(word.length() == 0 && abbr.length() == 0) return true;
            if(word.length() == 0 || abbr.length() == 0) return false;
            
            int m = 0; int n = 0;
            int digit = 0;
            
            while(m < word.length() && n < abbr.length())
            {
                if(abbr.charAt(n) >= '0' && abbr.charAt(n) <= '9')
                {
                    if(abbr.charAt(n) == '0' && digit == 0) return false;   // digit starts with 0
                    digit = 10*digit + abbr.charAt(n) - '0';
                    n++;
                    if(n == abbr.length())                                  // if abbr ends, word has to end at the same time
                    {
                        return m + digit == word.length();
                    }
                    
                    
                }
                else
                {
                    m += digit;
                    digit = 0;
                    if(m == word.length())
                    {
                        
                        if(n == abbr.length()) return true;                 // both end
                        
                        else return false;                                  // word ends, abbr not
                    }
                    if(m > word.length()) return false;                     // abbr is longer than word
                    if(word.charAt(m) != abbr.charAt(n)) return false;      // match fails
                    else                                                    // go on...
                    {
                        m++;
                        n++;
                    }
                    
                }
            }
            
                    //not even same length
            return m >= word.length() && n >= abbr.length();
            
     
            
                
            
        }
    }
    
  • 相关阅读:
    sql2005 如何重启数据库服务
    jQuery 树控件 zTree
    谈C#中的Delegate
    微博首席架构师杨卫华:新浪微博技术架构分析(转)
    jqGrid 各种参数 详解
    asp.net(c#)ref,out ,params的区别
    gcc
    数据结构递归
    跳表
    javajvm
  • 原文地址:https://www.cnblogs.com/reboot329/p/5931832.html
Copyright © 2011-2022 走看看