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. percona-toolkit 3.0.13 简单安装记录

    percona-toolkit 3.0.13 简单安装记录 环境:centos6.x mysql:8.0.17 yum -y install perl-DBIyum -y install perl-D ...

  2. SQL 以逗号分隔查询;调用自定义函数

    select col from [dbo].[GetInPara]('101,102,103',',') USE [xxx] GO /****** Object: UserDefinedFunctio ...

  3. 用Java 实现断点续传 (HTTP)

    在web项目中上传文件夹现在已经成为了一个主流的需求.在OA,或者企业ERP系统中都有类似的需求.上传文件夹并且保留层级结构能够对用户行成很好的引导,用户使用起来也更方便.能够提供更高级的应用支撑. ...

  4. 箱排序(Bin Sort)

    1.基本思想 排序过程无须比较关键字,而是通过"分配"和"收集"过程来实现排序.它们的时间复杂度可达到线性阶:O(n). 箱排序也称桶排序(Bucket Sor ...

  5. codevs 1002 搭桥x

    题目描述 Description 有一矩形区域的城市中建筑了若干建筑物,如果某两个单元格有一个点相联系,则它们属于同一座建筑物.现在想在这些建筑物之间搭建一些桥梁,其中桥梁只能沿着矩形的方格的边沿搭建 ...

  6. cvtColor

    E:/OpenCV/opencv/sources/modules/imgproc/src/color.cpp CV_RGB2GRAY:RGB--->GRAY.

  7. 更好的构建 Node 服务的工具

    更好的构建 Node 服务的工具 无论前端项目在打包后都发送给后台, 有时候自己想看看效果在运行 npm run build 后只是看到一个 build 文件夹,但是直接打开是无法浏览,因此需要开启一 ...

  8. 【转载】opencv 二值化函数——cv2.threshold

    https://blog.csdn.net/weixin_38570251/article/details/82079080 threshold:固定阈值二值化, ret, dst = cv2.thr ...

  9. 大数据笔记(三十)——一篇文章读懂SparkSQL

    Spark SQL:类似Hive ======================================================= 一.Spark SQL基础 1.什么是Spark SQ ...

  10. vue php实战项目开发(一)

    说在前面 项目开发过程中一直在学习,所以记录的过程可能比较乱,打算前台使用vue,后台使用php开发手机端的项目,这里记录遇到的问题和学习的过程 phpstudy环境下搭建虚拟域名,并开启URL重写 ...