单词缩写集 · word abbreviation set
[抄题]:
一个单词的缩写根据以下的形式。下面是一些缩写的例子
a) it --> it (没有缩写)
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
假设你有一个字典和给你一个单词,判断这个单词的缩写在字典中是否是唯一的。当字典中的其他单词的缩写均与它不同的时候, 这个单词的缩写是唯一的.
[暴力解法]:
1把缩写全部存一遍,再一个个搜索是否为重复,不重复unique
时间分析:
空间分析:
[思维问题]:
2有单词重复但是缩写相同的情况,此时仍为unique。
但是分类讨论也不好,把两种unique合并:单词出现次数和缩写出现次数相同
[一句话思路]:
把原单词和缩写分别放在两张哈希表来查,不要一起查。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 要把字符串拼起来时,直接用+即可。取出字母用的是charAt(i)的方法
- hash用getOrDefault(d, 0)+1来存数,记得加括号写0
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 返回单词可以用"" +的形式来直接拼接
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
hashmap:单词+出现次数,重要的是单词。用两个来比较出现的次数是否相同。
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
很多abbreviation的题。应该都是单独分离一个abbr函数,再用“”空格来拼接
[代码风格] :
- 整体的结构是类中包括成员变量+多个方法,不要把成员变量写在某一个方法里
public class ValidWordAbbr {
/*
* @param dictionary: a list of words
*/
HashMap<String, Integer> dict = new HashMap<>();
HashMap<String, Integer> abbr = new HashMap<>();
public ValidWordAbbr(String[] dictionary) {
for (String d : dictionary) {
dict.put(d, dict.getOrDefault(d, 0) + 1);//
}
for (String d : dictionary) {
abbr.put(getAbbr(d), abbr.getOrDefault(getAbbr(d), 0) + 1);
}
}
/*
* @param word: a string
* @return: true if its abbreviation is unique or false
*/
public boolean isUnique(String word) {
return (dict.get(word) == abbr.get(getAbbr(word)));
}
//getAbbr
private String getAbbr(String word) {
if (word.length() <= 2) {//<=
return word;//
}
return "" + word.charAt(0) + (word.length() - 2) + word.charAt(word.length() - 1);
}
}
/**
* Your ValidWordAbbr object will be instantiated and called as such:
* ValidWordAbbr obj = new ValidWordAbbr(dictionary);
* boolean param = obj.isUnique(word);
*/
单词缩写集 · word abbreviation set的更多相关文章
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- [LeetCode] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- P1624 单词缩写
P1624 单词缩写 题目描述 树树发现好多计算机中的单词都是缩写,如GDB是全称Gnu DeBug的缩写.但是,有时候缩写对应的全称会不固定,如缩写LINUX可以理解为: (1) LINus’s U ...
随机推荐
- mybatis异常:Could not find result map Java.util.Map 问题分析及解决 定位不到具体的位置的错误
mybatis异常:Could not find result map Java.util.Map 问题分析及解决 报这个错误呢,很难受的就是你定位不到具体的地方,找不到位置修改,你只知道有错误,但是 ...
- [CF1016G]Appropriate Team
codeforces description 给你一个数组\(\{a_i\}\)以及\(X,Y\),问你有多少对\((i,j)\)满足存在一个\(v\in \mathbb{N}^+\)使得 \(\gc ...
- 0302 IT行业就业与软件工程
阅读以下文章 http://www.thea.cn/news/terminal/9/9389.html http://www.shzhidao.cn/system/2015/09/22/0102610 ...
- oracle中union和minus的用法【oracle技术】
UNION是将两个或者两个以上的搜索结果集合并在一起!这个合并是有条件滴!记录的类型要匹配啦,记录的列数要一样啦!看看下面简单的例子: 有的朋友会说为什么要用union呢,直接用txt3 in ('I ...
- opencrud graphql 数据操作指南
opencrud 是社区团队提出,同时prisma框架就是按照这个标准设计的,里面包含了对于graphql 数据 操作的最佳实践,目前还在完善中,但是设计以及指南覆盖的功能还是比较全的,如果用过 pr ...
- ambassador 学习六 Module说明
模块允许给与特定的mapping 或者整体添加特定的行为,方便进行系统的控制. 当前的module 定义主要是系统级别的 当前系统主要的配置 --- apiVersion: ambassador/v0 ...
- lerna基本试用
安装 yarn global add lerna or npm install -g lerna 基本项目 初始化 git init platform cd platform lerna init 效 ...
- 微信跳一跳,Python辅助自动跳程序
一.说明 此代码借鉴一位大神提供在gitHub上的源码,已经做了简化合并处理,成功连上手机并运行后,可实现自动玩微信跳一跳游戏,刷个1000+的分数轻轻松松 github源码地址 https://gi ...
- cocos2dx ui显示机制
实验1 1,a.addChild(b); a的宽高没变,还是自己的宽高. 层级添加 不会改变原层大小. 2.node.addChild(sprite);node的宽和高也没变 感觉2dx的显示不是树 ...
- [C++ Primer] 第9章: 顺序容器
顺序容器概述 顺序容器的类型有: 类型 说明 vector 可变长度数组. 支持快速随机访问. deque 双端队列. 支持快速随机访问. list 双向链表. 只支持双向顺序访问. forward_ ...