[抄题]:

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation.

A string such as "word" contains only the following valid abbreviations:

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

Notice that only the above abbreviations are valid abbreviations of the string "word". Any other string is not a valid abbreviation of "word".

Note:
Assume s contains only lowercase letters and abbr contains only lowercase letters and digits.

Example 1:

Given s = "internationalization", abbr = "i12iz4n":

Return true.

Example 2:

Given s = "apple", abbr = "a2e":

Return false.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

  1. 第零位长度就是了,指针要先加再给 ++i 提前加一 , 需要再次控制范围:j < abbr.length() 否则会不自觉溢出
  2. 此题特殊:j所在数字为0也不行,会返回true
    "a"
    "01"

[思维问题]:

有两个单词居然会想不出两个指针吗?

[一句话思路]:

  1. 没数字时一直走,走到有数字时再开始统计

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

两个单词就用两个指针,两个起点

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

ASCII码表的顺序是(按二进制排序):数字-大写字母-小写字母

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

527. Word Abbreviation 自制单词压缩:要排序

411. Minimum Unique Word Abbreviation 排列组合找出第一个单词的所有缩写:回溯法

[代码风格] :

class Solution {
public boolean validWordAbbreviation(String word, String abbr) {
//ini
int i = 0, j = 0; //while loop
while (i < word.length() && j < abbr.length()) {
//if letters, go on
if (word.charAt(i) == abbr.charAt(j)) {
++i;
++j;
continue;
}
//cc, first num shouldn't be 0
if (abbr.charAt(j) <= '0' || abbr.charAt(j) > '9') return false;
//substring of j
int start = j;
//control j whenever
while (j < abbr.length() && abbr.charAt(j) >= '0' && abbr.charAt(j) <= '9') {
++j;
}
int num = Integer.valueOf(abbr.substring(start, j));
//add to i
i += num;
} //return
return (i == word.length()) && (j == abbr.length());
}
}

408. Valid Word Abbreviation有效的单词缩写的更多相关文章

  1. 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...

  2. [LeetCode] Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  3. [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  4. 408. Valid Word Abbreviation

    感冒之后 睡了2天觉 现在痊愈了 重启刷题进程.. Google的题,E难度.. 比较的方法很多,应该是为后面的题铺垫的. 题不难,做对不容易,edge cases很多,修修改改好多次,写完发现是一坨 ...

  5. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  6. Leetcode: Valid Word Abbreviation

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  7. [LeetCode] 408. Valid Word Abbreviation_Easy

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  8. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  9. [LeetCode] Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

随机推荐

  1. HDU - 6183:Color it (线段树&动态开点||CDQ分治)

    Do you like painting? Little D doesn't like painting, especially messy color paintings. Now Little B ...

  2. 无线网破解 跑字典 EWSA使用教程

    当我们用奶瓶抓到包,就可以再windowsxp&7&8下跑字典 工具/原料 EWSA4.0完美汉化破解版 字典 方法/步骤 1.打开程序 2.导入握手包 3.配置EWSA ,1.选项 ...

  3. Doxygen—程序文档生成工具

    Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,完全支持C.C++.Java.Objective-C和IDL语言,部分支持PHP.C#.注释的语法与Qt-Doc.KDoc和J ...

  4. webpack新版本4.12应用九(配置文件之入口和上下文(entry and context))

    entry 对象是用于 webpack 查找启动并构建 bundle.其上下文是入口文件所处的目录的绝对路径的字符串. context string 基础目录,绝对路径,用于从配置中解析入口起点(en ...

  5. Windows下安装Object C开发环境,及Hello Word(转)

    Windows下安装Object C开发环境,及Hello Word 最近想学习iphone开发,但是由于没有c基础,只有java基础.所以先从基础学习,首先是搭建环境,目前手头没有mac机子,只能先 ...

  6. oscache源码浅析

    oscache作为本地缓存框架,存储模型依然是通用的缓存键值对模型.oscache使用HashTable存放数据,我们看下源码: GeneralCacheAdministrator: /** * Ge ...

  7. Oracle查询多边形对象SDO_GEOMETRY并转换为java对象举例

    最近实现了一个判断点是否与多边形交互的功能,这里的点是一个经纬度,多边形是一个区域,包含多个经纬度,最后看下这个点是否在这个区域内.就好比你打开百度地图,然后看你自己的位置(点)是不是在某个小区(多边 ...

  8. windows64位Oracle安装和PL/SQL配置

    折腾了一下午,趟了几个坑,终于用PL/SQL连上了Oracle,晒晒填坑经历. 先去oracle官网下数据库安装文件,官网有登陆验证,如果没注册的话先注册吧.数据库(下载地址http://www.or ...

  9. MVC 控制器之间传值学习——session

    刚接触MVC不久,写的一些代码自己都不忍心看下去.路漫漫其修远兮,宝宝还需努力!之前只用过Session做登录时用户信息的储存,今天对集合类数据做了小小的尝试:利用session在控制器之间传值,以减 ...

  10. [转载]交换机STP协议

    注:之前做一个项目,测试部使用2个公司的交换机,H3C和H公司的,H公司的交换机是OEM H3C的交换机,正常来说两者使用没有区别. 但是使用中发现,如果设备的多个对外业务网口连接的交换机的聚合网口, ...