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. DW1000芯片定位技术解析

    近些年来随着物联网和机器人技术的大发展,精确定位技术的热度也随之攀升.目前精确定位的技术有很多,如基于wifi.RFID.zigbee.超声波.UWB等技术都可以实现精准定位.由于技术的不同,精度也不 ...

  2. java虚拟机之JVM生命周期

    java生命周期分为以下三部分:启动,运行,消亡. 启动.启动一个Java程序时,一个JVM实例就产生了,任何一个拥有public static void main(String[] args)函数的 ...

  3. 【LeetCode】解码方法

    [问题] 一条包含字母 A-Z 的消息通过以下方式进行了编码:'A' -> 1'B' -> 2…'Z' -> 26给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 : 输 ...

  4. 【LeetCode】分发糖果

    [问题]老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果.相邻的孩子中 ...

  5. 第二阶段scrum-1

    1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 注册登陆界面功能正在实装,消息收发功能正在制作 雷达界面已经完成.

  6. python人脸识别项目face-recognition

    该项目基于Github上面的开源项目人脸识别face-recognition,主要是对图像和视频中的人脸进行识别,在开源项目给出的例子基础上对视频人脸识别的KNN算法进行了实现. 0x1 工程项目结构 ...

  7. DevOps专题|Lua引擎打造超轻量级客户端

    Lua 作为一门轻量级脚本语言,源码使用标准C语言发布,语法简洁,非常适合嵌入式.客户端.游戏等场景. Lua引擎语言特点 轻量级 源码简单,以lua最新版5.3.5为例,加上lua自身提供的lib库 ...

  8. web系统能力培养计划

    服务器知识掌握如下 01购买linux服务器 客户端工具:https://mobaxterm.mobatek.net/download.html 02linux常用命令 https://www.run ...

  9. MySQL的异常问题

    异常问题

  10. java使用mongoTemplate去重排序查询

    import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.c ...