[抄题]:

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. Linux 安全rm

    先将shell脚本放在某个全局路径下,如/usr/local/bin #!/bin/sh # safe rm # Don't remove the file, just move them to a ...

  2. HDU - 6314:Matrix (广义容斥)(占位)

    Samwell Tarly is learning to draw a magical matrix to protect himself from the White Walkers. the ma ...

  3. SSM框架——Spring+SpringMVC+Mybatis的搭建

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  4. 《DSP using MATLAB》示例Example7.19

    代码: M = 33; alpha = (M-1)/2; Dw = 2*pi/M; l = 0:M-1; wl = Dw*l; %Hdr = [0, 0, 1, 1]; wdl = [0, 0.6, ...

  5. niosii dma实验中的一点感想

    1,使用nios给出的驱动函数的顺序一般为1,清中断2,写控制寄存器,3,写参数寄存器4,中断注册,5,开始工作.因为开始工作控制位在控制寄存器中,所以会想到到最后一块写,省事,但是在dma试验中发现 ...

  6. c语言中指针的一个小错误

    在定义指针后需要给指针赋值然后才能使用*p赋值或被赋值,这是个基础问题,没有理解,导致出问题. 空指针 ,也称悬 游指 针 ,是使 用 未初 始化 的指 针 .指针变量未初始化时它的值不是没有 ,而是 ...

  7. ASM概述

    ASM的全称是 Automatic Storage Management,ASM 是为存放oracle 数据文件而设计的一个volume manager 和 文件系统 管理的技术. ASM 支持ora ...

  8. 第12篇 PSR-1规范

    这个规范也不多,七点如下: 1. Overview Files MUST use only <?php and <?= tags. Files MUST use only UTF-8 wi ...

  9. erlang热部署

    以下流程参考rebar的wiki,亲测 rebar的版本一定要注意,高版本对于下面两个指令有bug rebar generate-appups rebar generate-upgrade 经过一个个 ...

  10. oracle 索引,组合索引

    1. 组合索引 id,code      组合 id,number  组合 2. 排序cost 使用 id ,cost=0 使用 id+code  cost=0 使用 id+number  cost= ...