边工作边刷题:70天一遍leetcode: day 88
Unique Word Abbreviation
要点:
- 简单题,主要是理解题意。no other words have the same abbreviation as the given word意思就是如果没有同样的abbrev或者有但是只由dict中相同的word产生。
- 可以用True/False表示unique,同时用set来保存哪个word产生的abbrev
# 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
class ValidWordAbbr(object):
def __init__(self, dictionary):
"""
initialize your data structure here.
:type dictionary: List[str]
"""
self.countmap = {}
self.words = set(dictionary)
for w in self.words:
abw = self.getAbbre(w)
self.countmap[abw]=True if abw not in self.countmap else False
def isUnique(self, word):
"""
check if a word is unique.
:type word: str
:rtype: bool
"""
w = self.getAbbre(word)
return w not in self.countmap or (self.countmap[w] and word in self.words)
def getAbbre(self, word):
n = len(word)
if n<=2: return word
return word[0]+str(n-2)+word[n-1]
# Your ValidWordAbbr object will be instantiated and called as such:
# vwa = ValidWordAbbr(dictionary)
# vwa.isUnique("word")
# vwa.isUnique("anotherWord")
边工作边刷题:70天一遍leetcode: day 88的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
随机推荐
- HTML5的新特性
HTML5 中的一些有趣的新特性: 用于绘画的 canvas 元素 用于媒介回放的 video 和 audio 元素 对本地离线存储的更好的支持 新的特殊内容元素,比如 article.footer. ...
- jQuery中each()、find()、filter()等节点操作方法
1.each(callback) 官方解释: 返回值:jQuery 概述 以每一个匹配的元素作为上下文来执行一个函数. 意味着,每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元 ...
- mysql服务器的字符集
文章:http://www.cnblogs.com/fantiantian/p/3468454.html 的评论中有这样的文字: 谢谢沧海一滴的总结 在Linux中一般都是UTF-8字符集.我们在建数 ...
- [程序人生]前途无"亮‘’的大学
转眼之间就到大四了,今天晚上很迷茫,很纠结,想了好多,好多,真的,长大之后,自从第一次失恋之后,第一次会想到这么的多. 嗯,先自我介绍哈吧,我是从云南的大山里走出来的孩子,什么样的大山,就是到 ...
- 养只爬虫当宠物(Node.js爬虫爬取58同城租房信息)
先上一个源代码吧. https://github.com/answershuto/Rental 欢迎指导交流. 效果图 搭建Node.js环境及启动服务 安装node以及npm,用express模块启 ...
- abap 选择屏幕事件AT SELECTION-SCREEN
AT SELECTION-SCREEN (1).其实就像一个FORM,所以在这个事件里声明的变量都是局部变量. (2).根据SY-UCOMM这个系统变量可以判断用户的命令 (3).在这个事件里响应的是 ...
- MSCRM 修改 默认组织
目前组织管理器上的方法是无效的,期待MS打补丁吧. 下面介绍修改DB的方法: 1)打开Sql server management,找到MSCRM_config数据库,打开数据表:SystemUser和 ...
- eclipse 设置豆沙绿保护色,保护眼睛
1.菜单栏 windows–>preferences–>general–>editors–>text editors中 在Appearance color options下修改 ...
- IOS 网络浅析-(七 JSON解析之三方JSONKit)
在这个网络横行的时代......... 有没有小说的感觉,哈哈
- C语言-12-日期和时间处理标准库详细解析及示例
概述 标准库 提供了用于日期和时间处理的结构和函数 是C++语言日期和时间处理的基础 与时间相关的类型 clock_t,本质是:unsigned long typedef unsigned long ...