[抄题]:

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

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. 初始化一个static的Map变量

    第一种方法:static块初始化 public class Demo{ private static final Map<String, String> myMap; static { m ...

  3. [置顶] 【机器学习PAI实践十一】机器学习PAI为你自动写歌词,妈妈再也不用担心我的freestyle了(提供数据、代码

    背景 最近互联网上出现一个热词就是"freestyle",源于一个比拼rap的综艺节目.在节目中需要大量考验选手的freestyle能力,freestyle指的是rapper即兴的 ...

  4. ZetCode PyQt4 tutorial signals and slots

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  5. python print 控制台输出中文

    在pycharm里面的控制台用print输出信息,  本意想输出中文, 但是实际上是u\xxxx. 可以用这种方式: print("%s " % cn_string)

  6. 动态库中的soname

    soname( Short for shared object name) 其是应用程序加载dll 时候,其寻找共享库用的文件名.其格式为 lib + math+.so + ( major versi ...

  7. 【转】BeyondCompare软件使用

    使用BeyondCompare比较文件夹下的文件时,相同的文件内容,但显示为不相同 原文网址:http://www.cnblogs.com/huangtailang/p/3925157.html 主要 ...

  8. CentOS+Apache虚拟主机域名设置

    首先注释掉 DocumentRoot /var/www/html     <virtualhost 192.168.1.105>     DocumentRoot /home/wxwb   ...

  9. Dubbo 基础教程

    原文地址:Dubbo 基础教程 博客地址:http://www.extlight.com 一.前言 当服务越来越多时,容量的评估,小服务资源的浪费等问题逐渐显现,此时需要增加一个调度中心基于访问压力实 ...

  10. JAVA通过JDBC连接Oracle数据库详解【转载】

    JAVA通过JDBC连接Oracle数据库详解 (2011-03-15 00:10:03) 转载▼http://blog.sina.com.cn/s/blog_61da86dd0100q27w.htm ...