[抄题]:

一个单词的缩写根据以下的形式。下面是一些缩写的例子

a) it                      --> it    (没有缩写)

     1
b) d|o|g --> d1g 1 1 1
1---5----0----5--8
c) i|nternationalizatio|n --> i18n 1
1---5----0
d) l|ocalizatio|n --> l10n

假设你有一个字典和给你一个单词,判断这个单词的缩写在字典中是否是唯一的。当字典中的其他单词的缩写均与它不同的时候, 这个单词的缩写是唯一的.

[暴力解法]:

1把缩写全部存一遍,再一个个搜索是否为重复,不重复unique

时间分析:

空间分析:

[思维问题]:

2有单词重复但是缩写相同的情况,此时仍为unique。

但是分类讨论也不好,把两种unique合并:单词出现次数和缩写出现次数相同

[一句话思路]:

把原单词和缩写分别放在两张哈希表来查,不要一起查。

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

[画图]:

[一刷]:

  1. 要把字符串拼起来时,直接用+即可。取出字母用的是charAt(i)的方法
  2. hash用getOrDefault(d, 0)+1来存数,记得加括号写0

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

  1. 返回单词可以用"" +的形式来直接拼接

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

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

hashmap:单词+出现次数,重要的是单词。用两个来比较出现的次数是否相同。

[其他解法]:

[Follow Up]:

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

很多abbreviation的题。应该都是单独分离一个abbr函数,再用“”空格来拼接

[代码风格] :

  1. 整体的结构是类中包括成员变量+多个方法,不要把成员变量写在某一个方法里
public class ValidWordAbbr {
/*
* @param dictionary: a list of words
*/
HashMap<String, Integer> dict = new HashMap<>();
HashMap<String, Integer> abbr = new HashMap<>(); public ValidWordAbbr(String[] dictionary) {
for (String d : dictionary) {
dict.put(d, dict.getOrDefault(d, 0) + 1);//
}
for (String d : dictionary) {
abbr.put(getAbbr(d), abbr.getOrDefault(getAbbr(d), 0) + 1);
}
} /*
* @param word: a string
* @return: true if its abbreviation is unique or false
*/
public boolean isUnique(String word) {
return (dict.get(word) == abbr.get(getAbbr(word)));
} //getAbbr
private String getAbbr(String word) {
if (word.length() <= 2) {//<=
return word;//
}
return "" + word.charAt(0) + (word.length() - 2) + word.charAt(word.length() - 1);
}
} /**
* Your ValidWordAbbr object will be instantiated and called as such:
* ValidWordAbbr obj = new ValidWordAbbr(dictionary);
* boolean param = obj.isUnique(word);
*/

单词缩写集 · word abbreviation set的更多相关文章

  1. [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation

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

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

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

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

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

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

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

  5. [LeetCode] Word Abbreviation 单词缩写

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

  6. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  7. [LeetCode] 527. Word Abbreviation 单词缩写

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

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

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

  9. P1624 单词缩写

    P1624 单词缩写 题目描述 树树发现好多计算机中的单词都是缩写,如GDB是全称Gnu DeBug的缩写.但是,有时候缩写对应的全称会不固定,如缩写LINUX可以理解为: (1) LINus’s U ...

随机推荐

  1. Winform开发常用控件之TreeView菜单导航和权限用法

    TreeView一个很棒的控件,我们在做WEB开发时常常犯困的一个东东.当然这里介绍winform里面的用法唠. 先介绍几个属性吧,CheckBoxes设置为true的话树形节点前面会出现checkb ...

  2. [QT]加快qt编译:设置默认多核编译qt

    使用环境:win7 + QT Creator 4.2.1 + QT5.8 + MinGW5.3.0 32bit 设置默认多核编译qt  来源:http://stackoverflow.com/ques ...

  3. BZOJ2121: 字符串游戏(DP)(字符串删单词,求最多可以删去多少)

    2121: 字符串游戏 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 672  Solved: 376[Submit][Status][Discuss ...

  4. Bandicam下载 + 破解

    BANDICAM是一款屏幕游戏录制工具. 今天给大家详细介绍下它的下载和破解使用. 安装方法: 一.准备工作 1.官网下载最新版. https://www.bandicam.com/cn/ 2.下载注 ...

  5. flask第二十六篇——模板【控制语句】【2】

    如果你也在学flask,就请加船长的公众号:自动化测试实战 我们先补充一下for循环的知识,我们之前说过,flask是由Jinja2+sqlAlchemy+werkzeug组成的,我们现在学的控制语句 ...

  6. C语言使用pthread多线程编程(windows系统)一

    运行之前需要做一些配置: 1.下载PTHREAD的WINDOWS开发包 pthreads-w32-2-4-0-release.exe(任何一个版本均可)    http://sourceware.or ...

  7. 关于 ThinkPHP5 使用 getBy 字段名方式获取数据

    关于 ThinkPHP5 使用 getBy 字段名方式获取数据 有小伙半说怎么全文搜索都没有搜索到 getByName 之类的函数. 其实是在这里.

  8. java局部变量和临时变量

    局部变量:temp=1, 临时变量:return a+b 临时变量会有一点的性能优势 局部变量会比成员变量和静态成员变量有优势,改进的方法是吧成员变量和静态成员变量赋值在局部变量:https://bl ...

  9. PSI分析

    "SI是对多个TS流的描述,它包含了PSI" PSI只提供了单个TS流的信息,使接收机能够对单个TS流中的不同节目进行解码:但是,它不能提供多个TS流的相关业务,也不能提供节目的类 ...

  10. 32位汇编基础_内存_每个应用进程都会有自己独立的4GB内存空间

    1.每个应用进程都会有自己独立的4GB内存空间 这句话很多人听起来可能会很矛盾很不解. 例如,我的电脑只有2GB的内存,打开个软件机会占用4GB内存,而我的电脑内存只有2GB,显然不够用,但是为什么程 ...