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 ...
随机推荐
- LINUX (centos)设置IP地址,网关,DNS
首先:备份原始配置文件: [logonmy@logon ~]$ cd /etc/sysconfig/network-scripts/ [logon@logon network-scripts]$ pw ...
- NOIP模拟题 管道
题目大意 给定$n$个点的无向图,求它的$Dfs$序方案数$n\leq 18$ 题解 状压$Dp+$记忆化搜索. 设$F_{i,now}$表示到达$i$其中$now$集合代表的点集已经遍历过,还需要遍 ...
- mysql锁之Next-Key Locks
一个Next-key锁结合了行锁和gap锁. InnoDB执行一个行级别锁在这样的一个途径,那就是它搜索或者扫描一个表索引时,它设置共享或者独占锁在它遭遇的索引记录上.于是,行级锁是真实的行记录锁.一 ...
- CodeForces - 484BMaximum Value(hash优化)
个人心得:周测题目,一题没出,难受得一批.这个题目做了一个半小时还是无限WR,虽然考虑到了二分答案这个点上面了, 奈何二分比较差就想用自己的优化,虽然卡在了a=k*b+c,这里但是后面结束了这样解决还 ...
- 剑指offer-第五章优化时间和空间效率(数组中出现次数超过一半的数字)
题目:输入一个数组,找出一个数字,它在数组中出现的次数超过数组的一半. 题目规定如果可以改变数组中元素的位置. 思路1:如果数组是排序的,那么中间元素的位置不就是次数超过数组一半的元素吗?是的,因此我 ...
- 【LIUNX】目录或文件权限,权限授予
三个三个一组看: 1. 第一段表示文件所有者对此文件的操作权限 2. 第二段表示文件所有者所在组对些文件的操作权限 3. 第三段表示除上述两种外的任何用户/组对此文件的操作权限 r读取:4 w写入:2 ...
- 两种方式创建Maven项目【方式二】
1.不勾选Create a simple project,直接点击下一步 2.选择maven-archetype-webapp下一步 3.填写相关信息,点击下一步完成 4.此时会报一个jsp的错误.我 ...
- 从python爬虫引发出的gzip,deflate,sdch,br压缩算法分析
今天在使用python爬虫时遇到一个奇怪的问题,使用的是自带的urllib库,在解析网页时获取到的为b'\x1f\x8b\x08\x00\x00\x00\x00...等十六进制数字,尝试使用chard ...
- FTP上传失败报错227 Entering Passive Model (222,111,8,111,10,40)
昨天为了一个ftp问题折腾了一天.问题背景:原来有个接口涉及到上传文件,服务端更换了ftp服务器,我们这边需要刷新连接服务端的ip和端口配置,代码没动.联调环境和验收环境都测试通过,一到生产环境就歇菜 ...
- file_put_contents(): supplied resource is not a valid stream resource
在项目开发的过程中 自己想把输出和一些想要内容输出到日志文件中,便于查看 但是在输入的过程中报了这样一个错误: file_put_contents(): supplied resource is no ...