边工作边刷题: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 ...
随机推荐
- custom struts framework
1. Difference between stucts1 and struts2 struts1 : Servlet used as Controller , you can visit the S ...
- Ahjesus Nodejs01 环境搭建及运行
访问http://nodejs.org/,根据系统选择下载文件,我用的win7 64 安装一路下一步直到完成 运行cmd输入node -v查看是否安装成功 成功会显示版本号 到此环境搭建完毕 ==== ...
- Python tools for Visual Studio插件介绍
Python tools for Visual Studio是一个免费开源的VisualStudio的插件,支持 VisualStudio 2010,2012与2013.我们想要实现的是: ...
- 面向企业客户的制造业CRM系统的不成熟思考
CRM就是客户关系管理(Customer Relationship Management),一直一知半解,最近有涉及这方面的需求,所以稍作研究,并思考一些相关问题. CRM是什么? CRM具体如何定义 ...
- (七)play之yabe项目【CRUD】
(七)play之yabe项目[CRUD] 博客分类: 框架@play framework 增加CRUD功能 使用CRUD能干嘛?----> 在页面对模型进行增删改查操作,这样有什么实际意义 ...
- eclipse导入svn项目,项目却没有svn的标记
现象: eclipse(已经装有svn插件)导入svn项目,项目没有svn的标记. 原因: 1.可能是由于你的svn eclipse插件,也就是subclipse,与svn的客户端版本不匹配. 解决 ...
- Egret Engine(白鹭引擎)介绍及windows下安装
Egret Engine简要介绍----- Egret Engine(白鹭引擎)[Egret Engine官网:http://www.egret-labs.org/]是一款使用TypeScript语言 ...
- 百度Couldn't load BaiduMapSDK_v2_4_1 from loader dalvik
原文链接:http://liuzhichao.com/p/1463.html 在使用百度定位SDK的时候,明明已经加入了liblocSDK3.so,Manifest中也添加了相应权限,注册了com.b ...
- 【读书笔记】iOS-防止通讯协议被轻易破解的方法
开发者可以选择类似Protobuf之类的二进制通讯协议或者自己实现通讯协议,对于传输的内容进行一定程度的加密,以增加黑客破解协议的难度. 参考资料: <iOS开发进阶> --唐巧
- 【读书笔记】iOS-UIWindow-WindowLevel
WindowLevel是UIWindow的一个属性.系统定义的一共有3种. UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal; UIKIT_EX ...