288. Unique Word Abbreviation
288. Unique Word Abbreviation
- 方法1:
- 方法2:
- 易错点
An abbreviation of a word follows the form . Below are some examples of word abbreviations:
a) it --> it (no abbreviation)1↓
b) d|o|g --> d1g1 1 11---5----0----5--8↓ ↓ ↓ ↓ ↓
c) i|nternationalizatio|n --> i18n11---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
方法1:
思路:
在constructor的时候就要把单词都存成hashmap, 那么value是什么呢?如果只对应着缩写对应单词的数量将不能区分door, door和door,deer。前一个是unique,后一个不是。还是要用一个set来记住所有单词,加上set本身有去重的功能,查询的时候只需要返回s.size()。
class ValidWordAbbr {public:ValidWordAbbr(vector<string> dictionary) {for (auto d: dictionary){if (d.size() < 3){hash[d].insert(d);}else {string abrev = *d.begin() + to_string(d.size() - 2) + *(d.end() - 1);hash[abrev].insert(d);}}}bool isUnique(string word) {int count = 0;string abrev;if (word.size() < 3) {abrev = word;}else {abrev = *word.begin() + to_string(word.size() - 2) + *(word.end() - 1);}for (auto w: hash[abrev]){if (w != word) count ++;}return count == 0;}
private:unordered_map<string, set<string>> hash;
};/*** Your ValidWordAbbr object will be instantiated and called as such:* ValidWordAbbr obj = new ValidWordAbbr(dictionary);* bool param_1 = obj.isUnique(word);*/
方法2:
思路:
为了压缩空间,同时还能区分同缩写单词是否是同单词,用了"消消乐" 的方法:value只保存unique的单词,如果在construct阶段进来的单词不同,两个词都消掉,设为“”。注意这个设置是有讲究的:它保证了count != 0, 同时又unqualified both。保证count != 0是因为,之后的isValid可能会询问之前不存在的缩写,应该算作unique。
易错点
- 消掉并恢复“”
- 自己不应该消掉自己:[“a”, “a”],“a” is still unique。
- isValid要检查未出现过的缩写
class ValidWordAbbr {public:ValidWordAbbr(vector<string> dictionary) {for (auto d: dictionary){string abrev;if (d.size() < 3){abrev = d;}else {abrev= *d.begin() + to_string(d.size() - 2) + *(d.end() - 1);}if (hash.count(abrev) == 0 || hash[abrev] == d) {hash[abrev] = d;}else {hash[abrev] = "";}}}bool isUnique(string word) {string abrev;if (word.size() < 3) {abrev = word;}else {abrev = *word.begin() + to_string(word.size() - 2) + *(word.end() - 1);}return hash.count(abrev) == 0 || hash[abrev] == word;}
private:unordered_map<string, string> hash;
};/*** Your ValidWordAbbr object will be instantiated and called as such:* ValidWordAbbr obj = new ValidWordAbbr(dictionary);* bool param_1 = obj.isUnique(word);*/
288. Unique Word Abbreviation相关推荐
- 288. Unique Word Abbreviation
题目: An abbreviation of a word follows the form <first letter><number><last letter> ...
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- 408. Valid Word Abbreviation
题目: Given a non-empty string s and an abbreviation abbr, return whether the string matches with the ...
- 继续过中等难度.0309
. 8 String to Integer (atoi) 13.9% Medium . 151 Reverse Words in a String 15.7% Mediu ...
- Python JAVA Solutions for Leetcode
Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...
- Leetcode重点250题
LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...
- 谷歌2017面经题集
发个Google onsite 面经给需要的人. round 1: 国人大哥, 出了道 merge N 个sorted element list的题, 要写成Generic的形式. 第二题是leetc ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
最新文章
- 使用设置报头x-Frame-Options限制iframe网页嵌套
- 图解Windows网络命令使用实例
- 傅里叶变换的初级理解三
- 关于CAShapeLayer的一些实用案例和技巧
- 一击进榜!达摩院十年“扫地僧”,揭秘阿里云数据仓库逆袭之旅
- Hybris Administration console功能一览
- linux下实现内存监视,shell脚本来监视Linux上的内存使用情况
- 如何在python官网下载pip_[Python]Pip的安装以及简单的使用
- 8/7-8/8-8/9 今日TF训练
- 基于SpringBoot+webSocket实现扫码登录功能
- ARToolKit从图片生成特征点数据
- 【单目标优化求解】基于matlab粒子群算法求解非线性目标函数最小值问题【含Matlab源码 1573期】
- 全网最全-固定资本存量分省、市、地区、产业-含计算过程
- MATLAB常用正则表达式记录
- Office EXCEL如何批量把以文本形式存储的数字转换为数字
- 【Ubuntu 20.04 LTS】设置笔记本合并盖子不休眠
- android源码编译1
- 目前所有ewebeditor版本***的漏洞与问题总结
- MSDN不能使用,提示“无法打开文档资源管理器”
- 地铁出行规划项目分析
热门文章
- merge和AMVP
- 运筹说 第66期|贝尔曼也有“演讲恐惧症”?
- 华为大数据HCIP认证(HCIP-Big Data Developer V2.0) 考试大纲
- gif图片过大怎么压缩?gif图怎么压缩大小?
- 背包三讲(感谢崔添翼 (Tianyi Cui)大佬的无私奉献)
- 你算过这笔账么?月薪5000在中国和美国的生活各是怎样?
- 水下导弹发射环境因素建模需求及其扫盲笔记
- 使用Portia时docker-compose失败 /bin/sh: 1: /app/provision.sh: Permission denied
- Ubuntu搭建EDK2环境
- Debian squeeze 美化字体