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的更多相关文章

  1. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  2. [LeetCode] 527. Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  3. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  4. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  5. Leetcode: Valid Word Square

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  6. Leetcode Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  7. 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...

  8. [LeetCode] Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  9. 408. Valid Word Abbreviation

    感冒之后 睡了2天觉 现在痊愈了 重启刷题进程.. Google的题,E难度.. 比较的方法很多,应该是为后面的题铺垫的. 题不难,做对不容易,edge cases很多,修修改改好多次,写完发现是一坨 ...

随机推荐

  1. Linux学习笔记(5)-hello world

    经过三天的熟悉,我已经将教程中那些常用命令都使用了一遍,所以,从今天起,我已经从一直Linux菜鸟蜕变成了大雕-- Linux的命令无穷多,要想背下来那肯定是不可能的,所以我的目标便是混个手熟,那些常 ...

  2. python 线程之threading(五)

    在学习了Event和Condition两个线程同步工具之后还有一个我认为比较鸡肋的工具 semaphores 1. 使用semaphores的使用效果和Condition的notify方法的效果基本相 ...

  3. [工作中的设计模式]享元模式模式FlyWeight

    一.模式解析 Flyweight在拳击比赛中指最轻量级,即“蝇量级”或“雨量级”,这里选择使用“享元模式”的意译,是因为这样更能反映模式的用意.享元模式是对象的结构模式.享元模式以共享的方式高效地支持 ...

  4. fork()创建子进程

    fork()系统调用是Unix下以自身进程创建子进程的系统调用,一次调用,两次返回,如果返回是0,则是子进程,如果返回值>0,则是父进程(返回值是子进程的pid) 在fork()的调用处,整个父 ...

  5. android Context 持有导致的内存泄漏

    Context使用场景 为了防止Activity,Service等这样的Context泄漏于一些生命周期更长的对象,可以使用生命周期更长的ApplicationContext,但是不是所有的Conte ...

  6. Working in Singapore

    这篇blog主要是想说说最近以及将来一年的时间需要在Singapore工作的感受.你可能以及猜到了,我现在写这篇blog是在Singapore的Office里面. 在一个月之前还在成都工作,每天9:0 ...

  7. CentOS7下安装chrome浏览器

    在CentOS 7环境下安装chrome浏览器 1.修改yum源 在/etc/yum.repos.d/目录下新建文件google-chrome.repo,向其中添加如下内容: [google-chro ...

  8. [转]关于AS3 Socket和TCP不得不说的三两事

    磨刀不误砍柴工,让我们从概念入手,逐步深入. 所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络 ...

  9. URL重写无效

    在IIS7或以上版本中使用urlrewriter时会出现无效的现像,这时候需要使用以下设置

  10. JS全兼容检测浏览器类型及版本

    直接上代码: <script> var browser = (function () { var isIE6 = /msie 6/i.test(navigator.userAgent); ...