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();
            
     
            
                
            
        }
    }
    
  • 相关阅读:
    BurpSuite—-Spider模块(蜘蛛爬行)
    BurpSuite系列(一)----Proxy模块(代理模块)
    hadoop HA集群搭建步骤
    HBase详解
    MapReduce两种执行环境介绍:本地测试环境,服务器环境
    HBase性能优化方法总结
    HDFS原理解析
    ZooKeeper 典型应用场景
    Redis总结
    基于Apache Curator框架的ZooKeeper使用详解
  • 原文地址:https://www.cnblogs.com/reboot329/p/5931832.html
Copyright © 2011-2022 走看看