408. Valid Word Abbreviation
感冒之后

睡了2天觉

现在痊愈了

重启刷题进程。。
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();
}
}
408. Valid Word Abbreviation的更多相关文章
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...
- [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 ...
- LeetCode Word Abbreviation
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...
- [LeetCode] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
随机推荐
- Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction policies
Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction pol ...
- 利用SpringMVC参数绑定实现动态插入数据
javabean代码:public class User { private String firstName; private String lastName; public String getF ...
- Lucene初步搜索
Lucene在创立索引后,要进行搜索查询 搜索大概需要5部, 1,读取索引. 2,查询索引. 3,匹配数据. 4,封装匹配结果. 5,获取需要的值. 语言表达能力不好,大概就是分着几部吧. /** * ...
- 一个在线的C++帮助文档网站 转载
http://www.cplusplus.com/ //C++参考 http://www.cppreference.com/wiki/start //C++参考 http ...
- 上传图片带预览功能兼容IE和火狐等主流浏览器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- javascript 一串DIV跟随鼠标移动
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Activity启动过程简要介绍
无论是通过点击应用程序图标来启动Activity,还是通过Activity内部调用startActivity接口来启动新的Activity,都要借助于应用程序框架层的ActivityManagerSe ...
- ubuntu下openGL的配置方法
This is a simple tutorial to show a new linux user (such as myself) how to setup freeglut and OpenGl ...
- bzoj2011: [Ceoi2010]Mp3 Player
Description Georg有个MP3 Player,没有任何操作T秒钟就会锁定,这时按下任意一个键就会变回没锁定的状态,但不会改变频道.只有在没锁定的状态下按键才有可能改变频道. MP3的频道 ...
- html5 中的 css样式单 的 两种调用方式的区别
在 html5 中 使用 css 样式单的方式 有4种: 1.链接外部样式文件:将样式文件 彻底与 html 文档分离,样式文件需要额外引入,这种情况下 一批样式 可以控制多份文档.对于好多文件都共有 ...