An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations:

a) it                      --> it    (no abbreviation)

     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

Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word's abbreviation is unique if no other word from the dictionary has the same abbreviation.

Example:

Given dictionary = [ "deer", "door", "cake", "card" ]

isUnique("dear") -> false
isUnique("cart") -> true
isUnique("cane") -> false
isUnique("make") -> true 解题关键点有3个:

1. 找出word abbreviation 的规律,<first letter><number><last letter>,number = string.length() - 2

2. 当发现dictionary 里有相同的abbreviation, key 对应的value 变为""

3. The abbreviation of "hello", i.e., h3o already exists in the dictionary.

Input: ["hello"],isUnique("hello") Output: [false] Expected: [true]

If the given word itself is in the dictionary, and it has the unique abbreviation, then we should return true.

 public class ValidWordAbbr {
private Map<String, String> map = new HashMap<String, String>(); public ValidWordAbbr(String[] dictionary) {
for(int i = ; i < dictionary.length; i++){
String key = abbreviate(dictionary[i]);
if(!map.containsKey(key)){
map.put(key, dictionary[i]);
}else{
map.put(key, "");
}
}
} private String abbreviate(String str){
return str.charAt() + Integer.toString(str.length() - )+ str.charAt(str.length()-);
} public boolean isUnique(String word) {
String x = abbreviate(word);
if(map.containsKey(x)){
if(map.get(x).equals(word)){
return true;
}else {
return false;
}
}
return true;
}
} // Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
// vwa.isUnique("Word");
// vwa.isUnique("anotherWord");

Unique Word Abbreviation的更多相关文章

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

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

  2. [Locked] Unique Word Abbreviation

    Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...

  3. Leetcode Unique Word Abbreviation

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

  4. 288. Unique Word Abbreviation

    题目: An abbreviation of a word follows the form <first letter><number><last letter> ...

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

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

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

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

  7. Unique Word Abbreviation -- LeetCode

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

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

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

  9. Leetcode: Minimum Unique Word Abbreviation

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

随机推荐

  1. css标签样式 边框 盒子模型 定位 溢出 z-index 透明度

    目录 一.CSS设置标签样式 1. 给标签设置长和宽 2.字体的颜色(3种表示方法) 3.文本样式 4. 语义 5. 背景样式 6. 边框 6.1 边框变圆 7.display 标签转换 二.盒子模型 ...

  2. java<T>泛型

    泛型 1.泛型的概述 在JDK1.5之前,把对象放入到集合中,集合不会记住元素的类型,取出时,全都变成Object类型.泛型是jdk5引入的类型机制,就是将类型参数化,它是早在1999年就制定的jsr ...

  3. qt5-工程文件的介绍--快捷键

    Ctrl+/    注释

  4. PL/SQL(Procedure Language & Structured Query Language)

    目前的PL/SQL包括两部分,一部分是数据库引擎部分:另一部分是可嵌入到许多产品(如C语言,JAVA语言等)工具中的独立引擎.可以将这两部分称为:数据库PL/SQL和工具PL/SQL. PL/SQL中 ...

  5. python 关于字节串和字符串

    import pickle s=pickle.dumps(clf) f=open('svm.txt','wb') #使用二进制方式打开,write进字节,否则进字符 f.write(s) f.clos ...

  6. linux lazarus 连接mssqlserver

    1 . 从https://www.freetds.org/ 下载驱动源文件 2. 参照 https://www.freetds.org/userguide/config.htm 内容编译 3. 启动l ...

  7. org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression:

    org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression:

  8. centos7下open--v!(p/n)部署

    一,client-server 路由模式 使用tun,openssl,lzo压缩,启用转发,生成证书,关闭selinux 同步下时间 #1安装 yum -y install openvpn easy- ...

  9. Anaconda是如何进行版本管理的?

    创建不同的environments,在电脑中会有不同的文件夹 然后,当使用conda下载时,会下载到不同的env文件夹下(提前进行env切换) 那么不是在anaconda prompt命令行下下载的呢 ...

  10. 【转】Django之Model层的F对象,Q对象以及聚合函数

    转自:https://blog.csdn.net/wsy_666/article/details/86692050 一.F对象: 作用:用于处理类属性(即model的某个列数据),类属性之间的比较.使 ...