边工作边刷题: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 ...
随机推荐
- python,python3
当下主流的Linux发行版都集成了python2,直接在终端输入$python就可进入交互式解释器,或者输入$python3进入python3的解释器,当然,也可以使用这个解释器直接翻译一个pytho ...
- java jdk environment variables
1. create system variable 2. edit the system path note: ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 3. cre ...
- MyBatis插入语句返回主键值
插入语句xml代码: <insert id="insertUser" parameterType="com.spring.mybatis.po.User" ...
- PHP学习笔记:利用百度api实现手机归属地查询
从来没有用过api,都不知道怎么获得api的数据,跟着demo,然后修改,终于实现了手机号码查询的功能,代码和说明很全,大家试试. <?php /** * Created by jianqing ...
- 编写运行R脚本
1.在后台运行R 1.1 创建file.R文件 1.2 在文件首行键入: #! /path/to/Rscript 1.3 在下面的行中,键入R代码 1.4 保存(记得有png(),jpeg(),... ...
- IOS6学习笔记(二)
四.使用关联引用为分类添加数据 虽然不能在分类中创建实例变量,但是可以创建关联引用(associative reference).通过关联引用,你可以向任何对象中添加键-值(key-value)数据. ...
- basket.js 源码分析
basket.js 源码分析 一.前言 basket.js 可以用来加载js脚本并且保存到 LocalStorage 上,使我们可以更加精准地控制缓存,即使是在 http 缓存过期之后也可以使用.因此 ...
- jQuery.merge()
jQuery.merge( first, second ) //返回Array 合并两个数组内容到第一个数组. first第一个用来合并的数组,元素是第二数组加进来的. second第二个数组合并到第 ...
- SharePoint 如何找到List的Template ID
在我们添加Ribbon操作,或者对特定模板进行操作的时候,经常需要ListTemplate的数值,我们经常需要搜索各种网页,来查找匹配的ListTemplate值,其实,有个比较简便的方法. 像定义R ...
- Fresco 使用笔记(一):加载gif图片并播放
项目总结 --------------------------------------------------------------------- 前言: 项目中图文混合使用的太多太多了,但是绝大部 ...