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.

这道题让我们验证单词缩写,关于单词缩写LeetCode上还有两道相类似的题目Unique Word AbbreviationGeneralized Abbreviation。这道题给了我们一个单词和一个缩写形式,让我们验证这个缩写形式是否是正确的,由于题目中限定了单词中只有小写字母和数字,所以我们只要对这两种情况分别处理即可。我们使用双指针分别指向两个单词的开头,循环的条件是两个指针都没有到各自的末尾,如果指向缩写单词的指针指的是一个数字的话,如果当前数字是0,返回false,因为数字不能以0开头,然后我们要把该数字整体取出来,所以我们用一个while循环将数字整体取出来,然后指向原单词的指针也要对应的向后移动这么多位数。如果指向缩写单词的指针指的是一个字母的话,那么我们只要比两个指针指向的字母是否相同,不同则返回false,相同则两个指针均向后移动一位,参见代码如下:

解法一:

class Solution {
public:
bool validWordAbbreviation(string word, string abbr) {
int i = , j = , m = word.size(), n = abbr.size();
while (i < m && j < n) {
if (abbr[j] >= '' && abbr[j] <= '') {
if (abbr[j] == '') return false;
int val = ;
while (j < n && abbr[j] >= '' && abbr[j] <= '') {
val = val * + abbr[j++] - '';
}
i += val;
} else {
if (word[i++] != abbr[j++]) return false;
}
}
return i == m && j == n;
}
};

下面这种方法和上面的方法稍有不同,这里是用了一个for循环来遍历缩写单词的所有字符,然后用一个指针p来指向与其对应的原单词的位置,然后cnt表示当前读取查出来的数字,如果读取的是数字,我们先排除首位是0的情况,然后cnt做累加;如果读取的是字母,那么指针p向后移动cnt位,如果p到超过范围了,或者p指向的字符和当前遍历到的缩写单词的字符不相等,则返回false,反之则给cnt置零继续循环,参见代码如下:

解法二:

class Solution {
public:
bool validWordAbbreviation(string word, string abbr) {
int m = word.size(), n = abbr.size(), p = , cnt = ;
for (int i = ; i < abbr.size(); ++i) {
if (abbr[i] >= '' && abbr[i] <= '') {
if (cnt == && abbr[i] == '') return false;
cnt = * cnt + abbr[i] - '';
} else {
p += cnt;
if (p >= m || word[p++] != abbr[i]) return false;
cnt = ;
}
}
return p + cnt == m;
}
};

类似题目:

Unique Word Abbreviation

Generalized Abbreviation

参考资料:

https://discuss.leetcode.com/topic/61404/concise-c-solution

https://discuss.leetcode.com/topic/61430/java-2-pointers-15-lines

https://discuss.leetcode.com/topic/61353/simple-regex-one-liner-java-python

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Valid Word Abbreviation 验证单词缩写的更多相关文章

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

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

  2. Leetcode: Valid Word Abbreviation

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

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

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

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

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

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

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

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

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

  7. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  8. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  9. Leetcode: Valid Word Square

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

随机推荐

  1. Oracle RAC 更换存储实验

    实验环境准备: RHEL 6.5 + Oracle 11.2.0.4 RAC (2nodes) OCR和Voting Disk使用的是OCR1磁盘组,底层对应3个1G大小的共享LUN,一般冗余: DA ...

  2. c#+handle.exe实现升级程序在运行时自动解除文件被占用的问题

    我公司最近升级程序经常报出更新失败问题,究其原因,原来是更新时,他们可能又打开了正在被更新的文件,导致更新文件时,文件被其它进程占用,无法正常更新而报错,为了解决这个问题,我花了一周时间查询多方资料及 ...

  3. VR/AR 非技术总结

    VR/AR 非技术总结 **欢迎转载~转载请注明Erma的博客 http://www.cnblogs.com/Erma-king/** 都说2016是VR/AR的元年,上半年我随着新技术的潮流进入了V ...

  4. UED双飞翼布局

    <style> body,html { height:%; padding: ; margin: } .main { background: #f2f2f2; width: %; floa ...

  5. 现有语言不支持XXX方法

    史上最强大的IDE也会有bug的时候哈,今天遇到这个问题特别郁闷,百度了下,果然也有人遇到过这个问题 解决方法: 1.调用的时候参数和接口声明的参数不一致(检查修改) 2.继承接口中残留一个废弃的方法 ...

  6. 查看Sql Server被锁的表以及解锁

    查看被锁表: select spId from master..SysProcesses where db_Name(dbID) = '数据库名称' and spId <> @@SpId ...

  7. 微信小程序开发教程

    9月21日晚发布的微信公众平台·小程序内侧邀请,微信应用号(小程序,「应用号」的新称呼)终于来了!目前还处于内测阶段,微信只邀请了部分企业参与封测.想必大家都关心应用号的最终形态到底是什么样子? 怎样 ...

  8. GJM : 常用网站收集 【不断更新中... ... ... 】

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  9. 炫酷的jQuery对话框插gDialog

    js有alert,prompt和confirm对话框,不过不是很美体验也不是很好,用jQuery也能实现, 体验效果:http://hovertree.com/texiao/jquery/34/ 代码 ...

  10. px-rem 一个将px转换为rem的工具

    将px转换为rem的工具,github地址:https://github.com/finance-sh/px-rem 怎样转换静态文件 安装: npm install px-rem -g 然后跑下命令 ...