408. 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.
链接:https://leetcode.com/problems/valid-word-abbreviation/#/description
3/22/2017
注意:
1. 第8行判断是否有leading的0,我认为这个属于invalid input。不过既然是google,需要仔细检查所有可能。
2. 遇到char时,index如何加,比较之后是否加。其实,在比较之后,之前inInteger的状态已经无所谓了,所以都需要+1
1 public class Solution { 2 public boolean validWordAbbreviation(String word, String abbr) { 3 boolean inInteger = false; 4 int number = 0; 5 int index = 0; 6 for (int i = 0; i < abbr.length(); i++) { 7 if (Character.isDigit(abbr.charAt(i))) { 8 if (!inInteger && abbr.charAt(i) - '0' == 0) return false; 9 number = number * 10 + abbr.charAt(i) - '0'; 10 inInteger = true; 11 } else { 12 if (inInteger) { 13 index += number; 14 inInteger = false; 15 number = 0; 16 } 17 if (index >= word.length()) return false; 18 if (word.charAt(index) != abbr.charAt(i)) return false; 19 index += 1; 20 } 21 } 22 if (inInteger) { 23 index += number; 24 } 25 if (index != word.length()) return false; 26 return true; 27 } 28 }
别人的思路:
1. 每次先比较2者是否相同,若不同查abbr是否是<=0或者>9(注意这里就排除了leading 0),再记录abbr index遍历abbr直到abbr的值不为数字,同时word加上中间的间隔。下次比较可以有结论。
2. 有个外国老哥,总是有很巧妙的方法:
1 public boolean validWordAbbreviation(String word, String abbr) { 2 return word.matches(abbr.replaceAll("[1-9]\\d*", ".{$0}")); 3 }
其他讨论:https://discuss.leetcode.com/category/535/valid-word-abbreviation
转载于:https://www.cnblogs.com/panini/p/6609229.html
408. Valid Word Abbreviation相关推荐
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- 288. Unique Word Abbreviation
288. Unique Word Abbreviation 方法1: 方法2: 易错点 An abbreviation of a word follows the form . Below are s ...
- 288. Unique Word Abbreviation
题目: An abbreviation of a word follows the form <first letter><number><last letter> ...
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Python JAVA Solutions for Leetcode
Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- UVa 642 - Word Amalgamation
题目:给你一个单词列表.再给你一些新的单词.输出列表中又一次排列能得到此新单词的词. 分析:字符串.对每一个字符串的字母排序生成新的传f(str).总体排序,用二分来查找就可以. 说明:注意输出要满足 ...
- hdu1113 Word Amalgamation(详解--map和string的运用)
版权声明:本文为博主原创文章.未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/35338617 转载请注明出 ...
最新文章
- Mac+docker+flask
- QRCode简介(收藏)
- 对文本的内容进行排序
- 同时启动多个Tomcat服务器
- 原理图端口符号_什么是电气原理图和电气接线图,二者区别及绘制要求是什么?...
- 第 10 章 Python 第三方库使用
- Android input touchpanel双击灭屏
- 计算机访问共享打印机无权限访问,Windows10电脑系统共享打印机无访问权限?详细解决步骤...
- Vue单文件不兼容IE解决方法
- java木马源码_用Java编写木马程序【附源代码下载】
- win10鼠标右键问题,导致桌面刷新重启,资源管理器explorer重启,文件夹闪退,应用管理员模式无法运行等等
- 2014广东计算机一级试题及答案,广东计算机一级试题2014版
- 腾讯手游助手吃鸡一直服务器繁忙,腾讯手游助手吃鸡手游常见问题解决办法介绍...
- Idea2023安装教程
- 安装Fedora(附镜像下载地址)
- 数据规模超大,分布密集,数据存储难
- 学习linux可以考虑做的岗位
- 开源企业内部沟通协作平台,ENTBOOST 发布 1.21 版本
- 输入一个字符,判断它是否为小写字母,如果是,将其装换成大写字母,否则,不转换
- 03.鸿蒙HarmonyOS卡片 状态栏透明和代码设置渐变色
热门文章
- python大作业外星人入侵_【python3小白上路系列】外星人入侵——开始
- 二叉搜索时与双向链表python_JZ26-二叉搜索树与双向链表
- 怎么把原来的墙拆掉_家装拆除不是简单的砸砸墙,它也是有技术的
- java jmap mat_java内存调优之jmap,jstack,mat
- 操作系统上机作业--使用系统调用实现mycp
- Java GregorianCalendar getTimeZone()方法与示例
- 嵌入式实训-day1
- Java——集合(模拟斗地主洗牌和发牌进行排序)
- 教主们:虽然这道题我不会,但Ac还是没问题的。
- 279. 完全平方数 golang BFS