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. CSS - input 美化

    input{ padding: 20px; width: 100%; height: 5vh; margin-bottom: 2vh; border-radius: 10vw; border: 0; ...

  2. 6 ~ express ~ 搭建用户注册前端页面

    一,前端页面 /views/login.html <!DOCTYPE html> <html lang="en"> <head> <met ...

  3. pip2 install protobuf==2.6.1

    [libprotobuf FATAL google/protobuf/stubs/common.cc:61] This program requires version 3.5.0 of the Pr ...

  4. bugku-杂项 听首音乐

    下载文件,是个wav文件,用Audacity打开,发现有 放大后记录下来:(每一组后面加上空格) ..... -... -.-. ----. ..--- ..... -.... ....- ----. ...

  5. RCE

    RCE remote command/code execute 远程系统命令/代码执行 系统从设计上需要给用户提供指定的远程命令操作的接口.可以测试一下自动运维平台. 在PHP中,使用system.e ...

  6. 201771010123汪慧和《面向对象程序设计Java》第十三周实验总结

     一.理论部分 1.GUI为用户提供交互式的图形化操作界面. (1)提供了程序的外观和感觉.(2)程序利用图形用户界面接受用户的输入,向用户输出程序运行的结果. 2.Java有专门的类库生成各种标准图 ...

  7. 2016蓝桥杯省赛C/C++A组第三题 方格填数

    题意:如下的10个格子  填入0~9的数字.要求:连续的两个数字不能相邻. (左右.上下.对角都算相邻) 一共有多少种可能的填数方案? 分析:dfs,划定边界,行1~4,列1~3,初始化为INT_IN ...

  8. POJ 3087:Shuffle'm Up

    Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7364   Accepted: 3408 Desc ...

  9. pytorch学习问题汇总

    问题六: 问题五:这里是怎么得到的? 问题四:为什么会是如下结果? torch.bernoulli(a)怎么是这个结果? 问题1:torch各个类型数据格式如何转换?数据类型在官方文档torch.Te ...

  10. java基础二 分支循环

    分支循环:    if...    if...else...    if...else if...    if...else if...else...    switch...case...defau ...