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();
            
     
            
                
            
        }
    }
    
  • 相关阅读:
    SSR 第二篇,搭建Vue SSR程序
    SSR 第一篇,搭建简单的SSR程序
    数组的所有方法整理学习
    CustomEvent 使用
    VUE CSS module
    eslint Expected linebreaks to be 'LF' but found 'CRLF'
    利用Object.freeze() 提升性能
    vue 项目开启gzip 压缩和性能优化
    [转]Ext Grid控件的配置与方法
    ExtJS renderer(转)
  • 原文地址:https://www.cnblogs.com/reboot329/p/5931832.html
Copyright © 2011-2022 走看看