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();
            
     
            
                
            
        }
    }
    
  • 相关阅读:
    Python面向对象编程
    Python模块
    Python函数式编程(把函数作为参数传入)
    Python函数高级特性
    Python函数基础
    连续数字或英文字符文本强制换行
    flex布局文本过长不显示省略号
    在div中放一个相同大小的svg,实际显示的位置svg偏下
    设置git push默认branch
    c# using
  • 原文地址:https://www.cnblogs.com/reboot329/p/5931832.html
Copyright © 2011-2022 走看看