Leetcode: 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.
public class Solution {
public boolean validWordAbbreviation(String word, String abbr) {
int i = 0, j = 0;
while (i<word.length() && j<abbr.length()) {
if (!isDigit(abbr.charAt(j))) {
if (word.charAt(i) != abbr.charAt(j)) return false;
i++;
j++;
}
else {
int num = 0;
while (j<abbr.length() && isDigit(abbr.charAt(j))) {
num = num*10 + (int)(abbr.charAt(j)-'0');
if (num == 0) return false; //"001" with '0' at front should return false
j++;
}
i = i + num;
}
}
if (i==word.length() && j==abbr.length()) return true;
return false;
}
public boolean isDigit(char c) {
if (c>='0' && c<='9') return true;
return false;
}
}
Leetcode: Valid Word Abbreviation的更多相关文章
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- Leetcode: Valid Word Square
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 【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 ...
- 408. Valid Word Abbreviation
感冒之后 睡了2天觉 现在痊愈了 重启刷题进程.. Google的题,E难度.. 比较的方法很多,应该是为后面的题铺垫的. 题不难,做对不容易,edge cases很多,修修改改好多次,写完发现是一坨 ...
随机推荐
- tornado 学习笔记8 模板以及UI
Tornado 包含一个简单.快速而且灵活的模板语言. Tornado同样可以使用任何其他的python模板语言,虽然没有集成这些模板语言进RequestHandler.ren ...
- 妙味课堂——HTML+CSS基础笔记
妙味课堂的课程讲得非常的清楚,受益匪浅.先把HTML和CSS基础课程部分视频的学习笔记记录如下: padding #PS基础 ##前端需要的PS技能 - PS技能(前端需要):切图.修图.测量 - P ...
- 更换app开发者账号
在开源中国上面有一个答案,http://www.oschina.net/question/2307266_237220 下面是我的执行步骤 首先在iTunes Connect中找到要更换开发者账号的a ...
- c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)
主界面
- 服务器部署多个tomcat经验
如果想在服务器上部署两个或多个tomcat项目,可以采用多个端口的方法: 如何修改Tomcat端口? 答:在Tomcat的conf文件夹里有个server.xml文件,修改里面的<Conne ...
- Join函数 及Split函数精解示例
'************************************************************************* '**模 块 名:Join函数 及Split函数精 ...
- PHP的变量
1.可变变量 一个变量的变量名可以动态地设置和使用.一个普通的变量通过声明来设置,而一个可变变量获取了一个普通变量的值作为这个可变变量的变量名,如下所示: <?php $hi = "h ...
- Visual Studio 2010编译时总是提示"调用目标发生了异常"的解决
现象: 无论建立的是Win32 Console的解决方案,还是MFC的解决方案,重新打开Visual Studio 2010之后,编译时总是提示“调用的目标发生了异常” 解决: 1. 关闭Visual ...
- 双十二前夕爆京东12G数据泄露的真相是什么
今天早上手机上推送出京东12g数据泄漏的消息,随即搜了下网上的相关新闻,感觉舆论又一次的干了一件惊天地的事情,到底京东的哪所谓的12G的用户信息数据有没有泄漏?舆论为什么齐刷刷的在12月11日突然间爆 ...
- CSP -- 运营商内容劫持(广告)的终结者
缘由 我们公司最近手机端H5 经常受到商户和用户的投诉,说有广告并且导致不能正常进行操作,我们商户自己当然不会加广告了,但是商户和用户可不管这些了,就认为是我们的问题 探索发现根本 目前我们用的很多浏 ...