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 ...
随机推荐
- C# 控制台程序 托盘图标 事件响应
static void Main(string[] args) { NotifyIconHelper ni = new NotifyIconHelper(); NotifyIconHelper.Sho ...
- js操作数据库实现注册和登陆
自从node-js出现之后,不只是java,php等后端语言可以操作数据库,进行内容的增删改查,javascript简本语言同样具备了该项技能,而且在node下,js具备了很强的操作性和代码的阅读性, ...
- [转] 属性选择器.mark
CSS 2 引入了属性选择器. 属性选择器可以根据元素的属性及属性值来选择元素. 简单属性选择 如果希望选择有某个属性的元素,而不论属性值是什么,可以使用简单属性选择器. 例子 1 如果您希望把包含标 ...
- excel快递单号查询工具以及源码
Function kdcx(kd, orderid) Dim Err, url, kdtime, link, Errcode, Status Select Case kd '此处支持的快递公司很多的 ...
- "git add -A" is equivalent to "git add .; git add -u".
git add -A stages All git add . stages new and modified, without deleted git add -u stages modified ...
- uitableview的空白处不能响应 touchesbegan 事件
现在的uitableview 的上面 响应不了 touchesbegan 事件 可能算是苹果的一个bug吧,不知道以后会不会改变 今天试了好久 都不行 最后 写了个字类 继承自 ...
- [Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】
题目链接:CF - R296 - d2 - D 题目大意 一个特殊的图,一些数轴上的点,每个点有一个坐标 X,有一个权值 W,两点 (i, j) 之间有边当且仅当 |Xi - Xj| >= Wi ...
- zoj 3841 Cards
题意:给你52张牌,已知一个牌的序列,然后利用剩余的牌,能排成多少个序列,这个序列比已知的序列字典序小. 思路:从左到右尽可能放比已知序列相应位置小,找不到就放一样,然后求组合数就可以.多重集排列定理 ...
- Android 通过开源框架AsyncHttpClient进行get和post请求
使用时无需将这些代码放入子线程去执行,因为其内部已经封装到一个线程中运行了! public void asyncHttpClientGet(View view) { AsyncHttpClient c ...
- Android与服务器端数据交互(http协议整合struts2+android)
在android中有时候我们不需要用到本机的SQLite数据库提供数据,更多的时候是从网络上获取数据,那么Android怎么从服务器端获取数据呢?有很多种,归纳起来有 一:基于Http协议获取数据方法 ...