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 ...
随机推荐
- jQuery 选择器【1】
jQuery 选择器 请使用我们的 jQuery 选择器检测器 来演示不同的选择器. 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname&q ...
- Net的struct的内存对齐问题
很少有人谈起struct的内存对齐问题, 就是在很多C#书中, 也很少提及. 但在实际应用中, 如果不注意内存对齐, struct比较大的话, 则会浪费一定的内存. 先从一个实例看起. publ ...
- JavaScript DOM编程艺术第二版学习(1/4)
接下来项目需要网页相关知识,故在大牛的指引下前来阅读本书. 记录方式:本书分四部分阅读,完成阅读之后会多写一篇包括思维导图的算是阅读指南的东西,浏览的童鞋看着指南可以跳过一些不必要的坑~ 当前水平:H ...
- 【转】关于C的未定义行为
关于C的未定义行为 转自:http://www.guokr.com/blog/471312/ 对于C的初学者来说,被要求做下面的这种题目真的是脑残的不能再脑残的行为.但是很多C初级教程——居然都有这样 ...
- Object之魔术函数__toString() 直接输出对象引用时自动调用
__toString()是快速获取对象的字符串信息的便捷方式 在直接输出对象引用时自动调用的方法. __toString()的作用 当我们调试程序时,需要知道是否得出正确的数据.比如打印一个对象时,看 ...
- Java实现单向链表
/* 先定义一个Node类用来存储节点的值域和指针域 * 即当前节点中的值和后面节点的方法 * 在C中就是相当与定义一个结构体类型一个数据域和指针域的方法 */class LNode{//这个写法已经 ...
- iOS:不同属性声明方式的解析
代码: /* 属性声明方式说明: ----------------------- 1 @interface ... { id name } @end 这样声明的属性其实可以认为是private属性,因 ...
- 10055 - Hashmat the Brave Warrior
Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave ...
- hadoop Yarn 编程API
客户端编程库: 所在jar包: org.apache.hadoop.yarn.client.YarnClient 使用方法: 1 定义一个YarnClient实例: private YarnClien ...
- Python 计算已经过去多少个周末
def weekends_between(d1,d2): days_between = (d2-d1).days weekends, leftover = divmod(days_between,7) ...