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");

Leetcode Unique Word Abbreviation的更多相关文章

  1. [LeetCode] 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] 527. Word Abbreviation 单词缩写

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

  5. [Locked] Unique Word Abbreviation

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

  6. 288. Unique Word Abbreviation

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

  7. Unique Word Abbreviation

    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. 【8-17】HTML测试

    Source : http://www.w3school.com.cn/html HTML 测验 结果:9/20 您的回答: 1.HTML 指的是? 您的回答:超文本标记语言(Hyper Text M ...

  2. Entity Framework实例详解

    Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...

  3. CSS文档流

    文档流 将窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素,即为文档流. 每个非浮动块级元素都独占一行, 浮动元素则按规定浮在行的一端. 若当前行容不下, 则另起新行再浮动. 内联元素也不 ...

  4. 欧几里得证明$\sqrt{2}$是无理数

    选自<费马大定理:一个困惑了世间智者358年的谜>,有少许改动. 原译者:薛密 \(\sqrt{2}\)是无理数,即不能写成一个分数.欧几里得以反证法证明此结论.第一步是假定相反的事实是真 ...

  5. 解析某些特殊格式XML文件时,获取不到根节点问题

    还是在语音识别这块.在读取本地的SRGS的XML后,无法获取到根节点<grammar>. 下面是SRGS.XML文件(只给出了根节点) <?xml version="1.0 ...

  6. Javascript面向对象编程一:基础篇

    该随笔分为以下四部分: Javascript面向对象编程一:基础篇 Javascript面向对象编程二:封装 Javascript面向对象编程三:继承 Javascript面向对象编程四:控件 先弄个 ...

  7. 1-File类的使用

    package com.io; import java.io.File; import java.io.FileInputStream; import java.io.IOException; imp ...

  8. C语言中的#define预处理指令

    本文链接:http://www.cnblogs.com/xxNote/p/4009460.html 今天看C Primer Plus里面看449页里面 16.2.1语言符号 讲到从技术方面看,系统把宏 ...

  9. H5图像遮罩-遁地龙卷风

    (-1)写在前面 这个idea不是我的,向这位前辈致敬.我用的是chrome49.用到的图片资源在我的百度云盘里http://yun.baidu.com/share/link?shareid=1970 ...

  10. linux 下 修改mysql账号密码

    1.root用户登录到mysql数据库代码示例:/usr/local/mysql/bin/mysql -u root -p (输入密码进入mysql)2.进入mysql,输入:代码示例:use mys ...