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. msbuild FileSysExcludeFiles

    <?xml version="1.0" encoding="utf-8"?> <!-- This file is used by the pu ...

  2. linux程序调试命令strace

    strace命令用法详解: strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须 ...

  3. yii2 小技巧

    参考地址:http://www.cnblogs.com/sandea/p/5714830.html 1.不通过日志获取AR执行的原生SQL语句和打印变量数据 $query = User::find() ...

  4. Xshell中文版使用说明/下载

    个人本来一直用SecureCRT的这几天朋友推荐用xshell就尝试下载了一个结果发现这个比SecureCRT和puttytel好使多了切更能更完善强大特此推荐附带注册码一枚可装999台机器 1309 ...

  5. oracle中substr与instr

    在oracle中,可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符.在一个字符串中查找指定的字符,返回被查找到的指定字符的位置. 语法: Instr(sourceString,de ...

  6. Windows疑难杂症之开机无法显示桌面。

    开机无法显示桌面可能有以下两种情况. 1.系统故障或病毒引起explorer.exe无法加载启动. 2.注册表故障造成默认的值不是explorer.exe.(可能是安装了某些软件造成此问题) 3,某开 ...

  7. ScriptManager与UpdatePanel总结

    1.From http://www.cnblogs.com/Tim-Seven/archive/2011/02/11/1952409.html Ajax Extensions 2.ScriptMana ...

  8. session 的用法

    </head> <body> <?php //session_start();//开启session,必须写在PHP代码最顶端 //HTTP,无状态性 //记录登陆者状态 ...

  9. 牡丹江.2014k(构造)

    K - Known Notation Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Su ...

  10. Spark之Streaming

    1. socket消息发送 import java.net.ServerSocket import java.io.PrintWriter import scala.collection.mutabl ...