[抄题]:

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation.

A string such as "word" contains only the following valid abbreviations:

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

Notice that only the above abbreviations are valid abbreviations of the string "word". Any other string is not a valid abbreviation of "word".

Note:
Assume s contains only lowercase letters and abbr contains only lowercase letters and digits.

Example 1:

Given s = "internationalization", abbr = "i12iz4n":

Return true.

Example 2:

Given s = "apple", abbr = "a2e":

Return false.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

  1. 第零位长度就是了,指针要先加再给 ++i 提前加一 , 需要再次控制范围:j < abbr.length() 否则会不自觉溢出
  2. 此题特殊:j所在数字为0也不行,会返回true
    "a"
    "01"

[思维问题]:

有两个单词居然会想不出两个指针吗?

[一句话思路]:

  1. 没数字时一直走,走到有数字时再开始统计

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

两个单词就用两个指针,两个起点

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

ASCII码表的顺序是(按二进制排序):数字-大写字母-小写字母

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

527. Word Abbreviation 自制单词压缩:要排序

411. Minimum Unique Word Abbreviation 排列组合找出第一个单词的所有缩写:回溯法

[代码风格] :

class Solution {
public boolean validWordAbbreviation(String word, String abbr) {
//ini
int i = 0, j = 0; //while loop
while (i < word.length() && j < abbr.length()) {
//if letters, go on
if (word.charAt(i) == abbr.charAt(j)) {
++i;
++j;
continue;
}
//cc, first num shouldn't be 0
if (abbr.charAt(j) <= '0' || abbr.charAt(j) > '9') return false;
//substring of j
int start = j;
//control j whenever
while (j < abbr.length() && abbr.charAt(j) >= '0' && abbr.charAt(j) <= '9') {
++j;
}
int num = Integer.valueOf(abbr.substring(start, j));
//add to i
i += num;
} //return
return (i == word.length()) && (j == abbr.length());
}
}

408. Valid Word Abbreviation有效的单词缩写的更多相关文章

  1. 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...

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

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

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

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

  4. 408. Valid Word Abbreviation

    感冒之后 睡了2天觉 现在痊愈了 重启刷题进程.. Google的题,E难度.. 比较的方法很多,应该是为后面的题铺垫的. 题不难,做对不容易,edge cases很多,修修改改好多次,写完发现是一坨 ...

  5. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  6. Leetcode: Valid Word Abbreviation

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  7. [LeetCode] 408. Valid Word Abbreviation_Easy

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  8. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  9. [LeetCode] Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

随机推荐

  1. c 可变参数(variable argument)的原理及使用

    本文主要介绍可变参数的函数使用,然后分析它的原理,程序员自己如何对它们实现和封装,最后是可能会出现的问题和避免措施. VA函数(variable argument function),参数个数可变函数 ...

  2. django初探-创建简单的博客系统(一)

    django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...

  3. c++ 遍历任务栏窗口

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h& ...

  4. 洛谷P2835 刻录光盘

    传送门 题目大意:有光盘可以传着看,问最少从哪几个人分发,能全部传一遍. 题解:缩点后求入度为0的点的个数 代码: #include<iostream> #include<cstdi ...

  5. weblogic11g的安装使用

    一.JDK的安装 下载linux环境下的jdk安装文件,本文资源下载:jdk-7u79-linux-x64.tar.gz 将jdk放到linux制定目录下,执行以下命令进行解压操作 tar -zxvf ...

  6. JDBC预编译语句表名占位异常

    有时候,我们有这样的需求,需要清空多个表的内容,这样我们有两种做法,可用delete from table 或 truncate table table,两种方法视情况而定,前者只是一条条的删除表数据 ...

  7. 关于bonecp和QuerRunner

    之前一直以为boneCP和QueryRunner是绑定的,但是其实不是,后者来自于commons-dbUtils,BoneCP就是负责连接池. while preparing SQL: UPSERT ...

  8. Delphi从Internet下载文件

    Delphi从Internet下载文件   今天在做拍卖系统的时候,因考虑到网络状况问题,需要将拍品所有信息下载到本机,包括拍品图片,因此需要实现从Internet下载文件的功能.      下面是代 ...

  9. OPCDAAuto.dll的C#使用方法浅析(转载)

    上次研究了.Net版本的OPC API dll,这次我采用OPCDAAuto.dll来介绍使用方法.以下为我的源代码,有详细的注释无需我多言.编译平台:VS2008SP1.WINXP.KEPServe ...

  10. java代码====实现按钮点击改变颜色=======

    总结: package com.a.b; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event ...