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. POJ 3984:迷宫问题 bfs+递归输出路径

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11844   Accepted: 7094 Description ...

  2. HDU 1003:Max Sum

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  3. POJ 3461:Oulipo

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28155   Accepted: 11251 Descript ...

  4. 用Python分析淘宝2000款避孕套,得出这些有趣的结论

    数据分析之前我们需要清楚的知道自己想要分析什么东西,也就是先搞清楚我们的目标.在公司可能是公司财报.用户增量变化.产品受欢迎程度.一些报表等等. 那我们今天的目标有哪些呢?我们来看看: ! 分析避孕套 ...

  5. vps 跑流量

  6. 实验吧Web-中-登陆一下好吗??

    题目上说:不要怀疑,我已经过滤了一切,还再逼你注入,哈哈哈哈哈! 可以试试,只要是输入的关键字都被过滤了,双写也被过滤掉了. 用万能密码发现,or被过滤掉了. 这里用到的是admin为:'=',密码为 ...

  7. 201903-1 小中大 Java

    思路: 中位数就是排序后中间的那个数.如果有偶数个数,就是中间两个数的平均值. 注意,这个平均值可能是整数,可能是小数,如果都是一样的处理,如果输出整数是3.0,而不是3,就有问题.所以需要分开处理. ...

  8. 【数据库】SQL 关系代数

    环境:MySQL ID:MySQL WorkBench 6.3 CE 实现以下有点难度的关系除法. 先给定义: 除运算是同时从关系的水平方向和垂直方向进行运算.给定关系R(X,Y)和S(Y,Z),X. ...

  9. nginx如何一个域名多个端口?

    方法一 写三个 listen server { listen 80; listen 81; listen 82; server_name www.sifou.com; ... 方法二 写三个serve ...

  10. SSM框架和微服务构架和的联系与区别

    spring和springMvc: 1. spring是一个一站式的轻量级的java开发框架,核心是控制反转(IOC)和面向切面(AOP),针对于开发的WEB层(springMvc).业务层(Ioc) ...