Word “book” can be abbreviated to 4, b3, b2k, etc. Given a string and an abbreviation, return if the string matches the abbreviation.

Assumptions:

  • The original string only contains alphabetic characters.
  • Both input and pattern are not null.
  • Pattern would not contain invalid information like "a0a","0".

Examples:

  • pattern “s11d” matches input “sophisticated” since “11” matches eleven chars “ophisticate”.
public class Solution {
public boolean match(String input, String pattern) {
// Write your solution here
int iIndex = 0;
int pIndex = 0;
char[] charArr = pattern.toCharArray();
while (iIndex < input.length() && pIndex < pattern.length()) {
char cur = charArr[pIndex];
if (Character.isLetter(cur)) {
if (cur != input.charAt(iIndex)) {
return false;
}
iIndex += 1;
pIndex += 1;
} else if (Character.isDigit(cur)) {
int num = 0;
while (pIndex < charArr.length && Character.isDigit(charArr[pIndex])) {
num = 10 * num + (int)(charArr[pIndex] - '0');
pIndex += 1;
}
iIndex += num;
}
}
return iIndex == input.length() && pIndex == pattern.length();
}
}

[Algo] 292. String Abbreviation Matching的更多相关文章

  1. Symbols of String Pattern Matching

    Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...

  2. [Algo] 649. String Replace (basic)

    Given an original string input, and two strings S and T, replace all occurrences of S in input with ...

  3. KMP string pattern matching

    The function used here is from the leetcode. Details can be found in leetcode problem: Implement str ...

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

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

  5. 关于limit hashlimit资料整理

    这几天正在捣鼓防火墙,用到了hashlimit模块.Google了一圈发现相关的文档无论英文还 是中文都很少, 所以我就把自己的折腾的心得记录下来吧. hashlimit是iptables的一个匹配模 ...

  6. iptables规则进阶

    iptables规则进阶 1.实验环境搭建: 在进行试验之前,先要进行实验环境的搭建,要求如下: 1.对于三台主机,分别设置IP地址 2.设置10.0.1.22为内网主机 3.设置中间主机有两个网卡, ...

  7. httpparase + httpclient 的运用

    这篇文章介绍了 HtmlParser 开源包和 HttpClient 开源包的使用,在此基础上实现了一个简易的网络爬虫 (Crawler),来说明如何使用 HtmlParser 根据需要处理 Inte ...

  8. 在C语言中利用PCRE实现正则表达式

    1. PCRE简介 2. 正则表达式定义 3. PCRE正则表达式的定义 4. PCRE的函数简介 5. 使用PCRE在C语言中实现正则表达式的解析 6. PCRE函数在C语言中的使用小例子 1. P ...

  9. Thinking in Java——笔记(13)

    Strings Immutable Strings Objects of the String class are immutable. Every method in the class that ...

随机推荐

  1. git 公钥 ssh

    1 安装 git 2 桌面右击  Git Bash Here 3 命令: git config --global user.name "git账户" git config --gl ...

  2. gentoo 修改键盘映射

    gentoo 上面修改键盘映射分为两种,一种是终端环境,一种是X环境. 终端环境 https://www.emacswiki.org/emacs/MovingTheCtrlKey https://wi ...

  3. 学习spring的第二天

    对昨天的查漏:关于<bean>标签的scope属性,是由它决定原型和单例的,而不是说你java代码中用到了单例模式就是单例了. 其二就是lazy-init属性,它对于scope=" ...

  4. js去除热点的虚线框

    1.一个页面有多张图片,图片的链接为热点绘制,在ie中点击会出现虚线框. <script type="text/javascript"> window.onload = ...

  5. psp --2

    PSP0 ---2 项目计划日志 姓名:赵腾                                                日期:9/12/2017 任务 日期 听课 编写程序 阅读课 ...

  6. Neo4j--图形理论基础

    参考 https://www.w3cschool.cn/neo4j/neo4j_graph_theory_basics.html 节点 圆圈表示的是节点.节点是图表的基本单位. 节点可以用来存储数据, ...

  7. 洛谷 P5661 公交换乘(队列)

    题目传送门 解题思路: 暴力模拟. AC代码: #include<iostream> #include<cstdio> #include<queue> using ...

  8. CTF - bugku-分析

    1.flag被盗 下载链接是.pcang文件 用wireshark打开 像这种流量分析题目,就要用Wireshark自带的搜索功能找尝试查找一些关键词(比如key.flag.shell.pass等) ...

  9. 使用Dom4j生成xml文件(utf-8编码)

    xml文件内容: <?xml version="1.0" encoding="UTF-8"?> <result> <code> ...

  10. ZOJ- 2562 反素数使用

    借用了下东北师大ACM的反素数模版. 本来我是在刷线段树的,有一题碰到了反素数,所以学了一下..有反素数的存在,使得一个x ,使得x的约数个数,在1 到 x的所有数里面,是最大的. 这里面还涉及安叔那 ...