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. location - 修改url后 - 重新加载

    window.location.href = window.location.pathname + search;

  2. 读《软件需求最佳实践》YOUGAN

    这几天在看<软件需求最佳实践>作者徐锋老师的软件需求培训,三天的课程,虽然原来对需求也关注了很多,自己也做过需求分析和开发的工作,但是这次培训感觉收获还是很多.三天的培训先做个记录,后续多 ...

  3. scanf与gets

    gets函数为什么不能读取字符就往下运行了 这里有一个共性的问题,就是 scanf 输入后,会遗留一个回车符,传递到下面的输入语句: 回车符就会被下面的输入语句接收,而结束了输入,这里就是一个错误的值 ...

  4. PAT 2018 春

    A 1140 Look-and-say Sequence 简单模拟.可能要注意字符串第一个字符和最后一个字符的处理. #include <cstdio> #include <iost ...

  5. VUE.js入门学习(3)-深入理解VUE组建

    1.使用组件的细节点 (1)is="模版名" (2)在子组建定义data的时候,data必须是一个函数,而不能是一个对象,每个子组建都有自己的数据存储.之间不会相互影响. (3)操 ...

  6. Bootstrap-按钮篇btn

    参考网址:http://v3.bootcss.com/(能抄不写) 1.按钮颜色样式: 对应代码:(主要体现在class内容:btn-default,btn-primary...) <butto ...

  7. Tensorflow学习教程------参数保存和提取重利用

    #coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mni ...

  8. QMainWindow的空间布局结构

    简单讲一下Qt的QMainWindow的结构,Qt的顶级窗口有三种类型,首先是万恶之源(...应该说是大部分控件的父类...)的QWidget,然后是QMainWidget和QDialog,后面两者也 ...

  9. 2020年使用Delphi的25个理由(我觉得四个优点:控件+可视化开发+跨平台+数据库,还有一个编译快,运行快)——人生苦短,我用Delphi!

    25年后从10个使用Delphi的理由到1个至25个使用Delphi 10.3的理由 25年前发布Delphi 1时,我汇总了使用Delphi的十大理由.这是我精通Delphi原始书的序言中的原始列表 ...

  10. win10查看显卡算力

    1.查看笔记本自带算力 运行C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\extras\demo_suite目录下的deviceQue ...