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很多,修修改改好多次,写完发现是一坨 ...
随机推荐
- 《DSP using MATLAB》示例Example5.13
- 浏览器-03 WebKit 渲染1
WebKit是一个渲染引擎,而不是一个浏览器; DOM是对HTML或者XML等文档的一种结构化表示方法,通过这种方式,用户可以通过提供标准的接口来访问页面中的任何元素的相关属性,并可对DOM进行相应的 ...
- Chart图表
这东西挺直观 封装个类 public class aaa { private string name; public string Name { get { return name; } set { ...
- 数学 SRM 690 Div1 WolfCardGame 300
Problem Statement Wolf Sothe and Cat Snuke are playing a card game. The game is played with exa ...
- lucene全文检索---打酱油的日子
检索内容,一般的程序员第一时间想到的是sql的like来做模糊查询,其实这样的搜索是比较耗时的.已经有lucene帮我们 封装好了,lucene采用的是分词检索等策略. 1.lucene中的类描述 I ...
- CodeForces 515C. Drazil and Factorial
C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 摸索探寻之Mac OS 使用及快捷键
刚发现的一个介绍Mac OS入门的帖子,新入手Mac的同学可以看看http://www.cnblogs.com/chijianqiang/archive/2011/08/03/2126593.html ...
- PHP实现快速排序、插入排序、选择排序
1.快速排序 快速排序(Quicksort)是对冒泡排序的一种改进.由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都 ...
- 【Linux】lsof 命令,记一次端口占用查询
3月21日测试时,发现测试服务器启,总是报端口占用情况,察看端口占用情况 1-使用命令 netstat -tunlp |grep 端口号 差看下 这个端口被那个进程占用 我当前使用的 JBOSS 端口 ...
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...