【LeetCode】408. Valid Word Abbreviation 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/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.
题目大意
给一个 非空 字符串 s 和一个单词缩写 abbr ,判断这个缩写是否可以是给定单词的缩写。
解题方法
双指针
一个指针指向abbr,一个指针指向word,根据abbr的数字情况移动word的指针,如果abbr指针指向的不是数字而是字符,那么判断是否和word字符相同。最后两个指针应该会同时移动到各自的结尾。
这个题有个小坑,就是有前导0的数字是非法的,需要判断一下。
C++代码如下:
class Solution {
public:
bool validWordAbbreviation(string word, string abbr) {
int iword = 0;
int iabbr = 0;
while (iword < word.size() && iabbr < abbr.size()) {
int count = 0;
if (count == 0 && abbr[iabbr] == '0')
return false;
while (abbr[iabbr] >= '0' && abbr[iabbr] <= '9') {
count = 10 * count + abbr[iabbr] - '0';
iabbr ++;
}
iword += count;
if ((iword >= word.size() && iabbr != abbr.size()) ||
(iword != word.size() && iabbr >= abbr.size()))
return false;
if (iword == word.size() && iabbr == abbr.size())
return true;
if (word[iword] != abbr[iabbr])
return false;
iword ++;
iabbr ++;
}
return iword == word.size() && iabbr == abbr.size();
}
};
日期
2019 年 9 月 19 日 —— 举杯邀明月,对影成三人
【LeetCode】408. Valid Word Abbreviation 解题报告(C++)的更多相关文章
- 【LeetCode】422. Valid Word Square 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- [LeetCode] 408. Valid Word Abbreviation_Easy
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- 【LeetCode】290. Word Pattern 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】916. Word Subsets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...
- 【LeetCode】127. Word Ladder 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...
- 【LeetCode】79. Word Search 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】320. Generalized Abbreviation 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
随机推荐
- R之dplyr::select/mutate函数扩展
select函数 dplyr包select函数用的很多,不过我们一般也是通过正反选列名或数字来选择列. 常见用法如: select(iris,c(1,3)) select(iris,1,3) #同上 ...
- GWAS在农业上应用
农业的组学技术应用虽然落后于人的研究,这是什么意义的问题,但有时农业基因组有自己无可比拟的优势,那就是材料.下面介绍GWAS应用. GWAS(Genome-wide association study ...
- 49-Reverse Linked List II
Reverse Linked List II My Submissions QuestionEditorial Solution Total Accepted: 70579 Total Submiss ...
- 02 Windows安装C语言开发工具CodeBlocks
CodeBlocks安装 使用微信扫码关注微信公众号,并回复:"C语言环境",免费获取下载链接! 1.卸载CodeBlocks(电脑未装此软件,跳过) 进入目录:C:\Pro ...
- 巩固javaweb第十四天
巩固内容: 单行文本框: 单行文本框的基本语法格式如下: < input type="text" name="输入信息的字" value=" ...
- JavaIO——内存操作流、打印流
我们之前所做的都是对文件进行IO处理,实则我们也可以对内存进行IO处理.我们将发生在内存中的IO处理称为内存流. 内存操作流也可分为两类:字节内存流和字符内存流. (1)ByteArrayInputS ...
- Linux学习 - 权限管理命令
一.chmod(change the permissions mode of a file) 1 功能 改变文件或目录权限 root 与 所有者 可进行此操作 2 语法 chmod [(ugoa) ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(13. 部署Jenkins/Maven实现代码自动化发布)
1. 简介 Jenkins: 官方网站:https://jenkins.io/ 下载地址:https://jenkins.io/download/ war包下载:http://mirrors.jenk ...
- Vue中如何书写js来渲染页面填充数据的部分代码
new Vue({ el:"#app" , data:{ user:{ id:"", username:"", password:" ...
- redis的总结笔记
# Redis 1. 概念: redis是一款高性能的NOSQL系列的非关系型数据库 1.1.什么是NOSQL NoSQL(NoSQL = Not Only ...