[抄题]:

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

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. 转:Android-apt

    转自http://blog.csdn.net/zjbpku/article/details/22976291 What is this? The Android-apt plugin assists ...

  2. 来来来,有讲一个吐血的故事(matlab)之脚本运行路径是什么

    脚本运行路径是什么,这真是太重要!! 重要1:你默认保存的路径 重要2:你访问的相对路径 先放图: 再看一幅图: 我的操作,点击左侧的文件夹,使上框的显示栏路径不一样,再点击运行,发现pwd指示的路径 ...

  3. PHP write byte array to file

    /********************************************************************************* * PHP write byte ...

  4. Linux的发行版之间的联系和区别

    转载:https://blog.csdn.net/suixin788/article/details/52555558 联系 Linux的内核源代码和Linux的应用程序都可以自由获得,因此很多公司组 ...

  5. Redis学习笔记-常用命令篇(Centos7)

    redis提供了丰富的命令,这些命令可以在linux终端使用.在各类语言中,这些命令都有对应的方法. 一.键值相关 1.keys 返回满足给定pattern的所有key 127.0.0.1:6379& ...

  6. 《DSP using MATLAB》示例Example 9.9

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. flow flow-typed 定义简单demo

    flow-typed 安装 全局 npm install -g flow-typed 测试代码 一个简单全局函数 目录根目录 flow-typed userLibDef.js declare func ...

  8. zipkin:HttpClient和struts

    因为要和老系统集成zipkin,意外的发现老系统使用的httpClient来发送信息.zipkin的官方demo可都是retstTemplate啊!有的搞头. 在看Demo的时候意外的发现其实其实2. ...

  9. GNU Radio: Overview of the GNU Radio Scheduler

    Scetion 1: The Flowgraph The flowgraph moves data from sources into sinks. 一个流图由多个模块组成,其中一般包括信源(Sour ...

  10. 基数排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def RadixSort(a): i = 0 #初始为个位排序 n = 1 #最小的位数置为1(包含0) max ...