408. Valid Word Abbreviation有效的单词缩写
[抄题]:
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]:
- 第零位长度就是了,指针要先加再给 ++i 提前加一 , 需要再次控制范围:j < abbr.length() 否则会不自觉溢出
- 此题特殊:j所在数字为0也不行,会返回true
"a"
"01"
[思维问题]:
有两个单词居然会想不出两个指针吗?
[一句话思路]:
- 没数字时一直走,走到有数字时再开始统计
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼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有效的单词缩写的更多相关文章
- 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 408. Valid Word Abbreviation
感冒之后 睡了2天觉 现在痊愈了 重启刷题进程.. Google的题,E难度.. 比较的方法很多,应该是为后面的题铺垫的. 题不难,做对不容易,edge cases很多,修修改改好多次,写完发现是一坨 ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- Leetcode: Valid Word Abbreviation
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] 408. Valid Word Abbreviation_Easy
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
随机推荐
- MySql必知必会实战练习(五)存储过程
1. 为什么使用存储过程? (1) 通过把处理封装在容易使用的单元中,简化复杂操作 (2) 为了保证数据的完整性,不要求反复建立一系列的处理操作,所有开发人员和应用程序都使用同一(试验和测试)存储过程 ...
- 重温CLR(五)类型和成员基础
类型的各种成员 类型可以定义以下种类的成员 1 常量 常量是指出数据值恒定不变的符号.这种符号使代码更易阅读和维护.常量总与类型管理,不与类型的实例管理.常量逻辑上总是静态成员. 2 字段 字段表示只 ...
- Servlet中的Filter(过滤器)
Filter,过滤器,是处于客户端与服务器资源文件之间的一道过滤网,在访问资源文件之前,通过一系列的过滤器对请求进行修改.判断等,把不符合规则的请求在中途拦截或修改.也可以对响应进行过滤,拦截或修改 ...
- UI多线程调用:线程间操作无效: 从不是创建控件"Form1"的线程访问它.
有两种方式解决 1.在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false;2.使用Invoke等委托函数. 问题原因是.net2.0以后拒绝多 ...
- 关于modelsim添加库的说明
声明:以下纯属个人习惯. 1.工程建好后添加一个编译好的库的方法是:file->new->library选择a map to an existing library.然后将这个库在你这工程 ...
- 戴尔PowerEdge RAID控制卡使用示例(PERC H710P为例)
Dell PERC使用示例列表(H710p) 特别说明,本文相关RAID的操作,仅供网友在测试环境里学习和理解戴尔PowerEdge服务器RAID控制卡的功能和使用方法.切勿直接在生产服务器上做相关实 ...
- 为什么中国出不了facebook和Twitter?
我们坐拥全球最大基数的网民,我们拥有让人骄傲的四大发明,我们有有流传全世界的孙子兵法,可是在互联网时代,我们却落后了.互联网可以说是江山人才辈辈出,各领风骚三两年. 让我们来简单地回顾一下近几年的互联 ...
- php 权限 管理
权限的思考: https://www.jianshu.com/p/cf9077a7d38a 权限例子,用户 角色 功能 用户角色关联表 角色功能关联表 http://www.cnblogs.com/n ...
- 如何在 C#中添加 dll 文件
按住鼠标左键,按住dll文件 ,然后将其拖动到工具箱里面 ,就出现了如图所示的控件
- mysql索引原理与慢查询优化2
七 正确使用索引 一 索引未命中 并不是说我们创建了索引就一定会加快查询速度,若想利用索引达到预想的提高查询速度的效果,我们在添加索引时,必须遵循以下问题 1 范围问题,或者说条件不明确,条件中出现这 ...