Unique Word Abbreviation
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的更多相关文章
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 288. Unique Word Abbreviation
题目: An abbreviation of a word follows the form <first letter><number><last letter> ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- Unique Word Abbreviation -- LeetCode
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- Leetcode: Minimum Unique Word Abbreviation
A string such as "word" contains the following abbreviations: ["word", "1or ...
随机推荐
- typing 模块
目录 typing模块 一.引言 二.typing模块的作用 三.使用typing模块 四.typing常用类型 typing模块 目录 一.引言 二.typing模块的作用 三.使用typing模块 ...
- SonarQube 7.7默认数据库连接方法
SonarQube7.7默认数据库为H2 embbed数据库 连接字符串:jdbc:h2:tcp://localhost:9092/sonar 用户名密码都为空
- 【知识】location.search获取中文时候会被编码成一串字符
[转码] 例如:case.html?id='这个是页面的标题' 当想要使用location.search获取?id='这个是页面的标题'的时候,包含的中文会被编码成一串字符串. 所以我们需要进行解码, ...
- 【gym102394A】Artful Paintings(差分约束系统,二分)
题意:给定一个长为n的序列,每个位置可以选择取或不取,要求构造方案使得: 1.对于前M1个约束,区间[L,R]内取的数量必须严格不少于K 2.对于后M2个约束,区间[L,R]外取的数量必须严格不少于K ...
- zabbix配置通过远程命令来发送邮件
1.安装好zabbix后,在/var/log/zabbix可以查看日志. 2.主机通过zabbix-get检查 yum install zabbix-get -y zabbix-get -s 客户主 ...
- BZOJ 2716 [Violet 3]天使玩偶 (CDQ分治、树状数组)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2716 怎么KD树跑得都那么快啊..我写的CDQ分治被暴虐 做四遍CDQ分治,每次求一个 ...
- 测试相关shell命令总结2——结构控制语句,命令行参数
1,shell 中单引号和双引号的区别,单引号不进行解释.双引号进行解释 1,在shell中进行数学运算,放在$和[]中 $[1+2] 有些很奇怪,在.sh文件中放在(())中貌似也能够进行数学运算. ...
- python 正则之字母匹配
\A:匹配字符串的开始 \b:匹配一个单词边界 取出a边界单词的个数 >>> len(re.findall(r"\ba"," ab abc ad ...
- Oracle JET mobile cordove navigator.app对象
在使用 Oracle JET 开发 webapp 时,会使用到 ojrouter ,ojrouter 默认含有历史记录推送功能.在调试 Android 时会发现返回键总是返回到上一次浏览记录(App ...
- VS2008重置默认环境
Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008命令提示 进入Common7\IDE,然后 ...