LeetCode Word Abbreviation
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/
题目:
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below.
- Begin with the first character and then the number of characters abbreviated, which followed by the last character.
- If there are any conflict, that is more than one words share the same abbreviation, a longer prefix is used instead of only the first character until making the map from word to abbreviation become unique. In other words, a final abbreviation cannot map to more than one original words.
- If the abbreviation doesn't make the word shorter, then keep it as original.
Example:
Input: ["like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"]
Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]
Note:
- Both n and the length of each word will not exceed 400.
- The length of each word is greater than 1.
- The words consist of lowercase English letters only.
- The return answers should be in the same order as the original array.
题解:
暴力解法把字典里所有的string都最短缩写,遇到重复的就都加个prefix再检查.
Time Complexity: O(mn). m = dict.size(). n是字典里string的平均长度.
Space: O(m).
AC Java:
class Solution {
public List<String> wordsAbbreviation(List<String> dict) {
int len = dict.size();
String [] res = new String[len];
int [] prefix = new int[len];
for(int i = 0; i<len; i++){
String abbr = getAbbr(dict.get(i), 0);
res[i] = abbr;
}
for(int i = 0; i<len; i++){
while(true){
HashSet<Integer> hs = new HashSet<Integer>();
for(int j = i+1; j<len; j++){
if(res[j].equals(res[i])){
hs.add(j);
}
}
if(hs.isEmpty()){
break;
}
hs.add(i);
for(int duplicateInd : hs){
res[duplicateInd] = getAbbr(dict.get(duplicateInd), ++prefix[duplicateInd]);
}
}
}
return Arrays.asList(res);
}
private String getAbbr(String s, int ind){
int len = s.length();
if(len-ind <= 3){
return s;
}
return s.substring(0, ind+1) + (len-ind-2) + s.charAt(len-1);
}
}
LeetCode Word Abbreviation的更多相关文章
- [LeetCode] Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [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] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode 527---Word Abbreviation
527. Word Abbreviation Given an array of n distinct non-empty strings, you need to generate minimal ...
随机推荐
- ZooKeeper分布式锁简单实践
ZooKeeper分布式锁简单实践 在分布式解决方案中,Zookeeper是一个分布式协调工具.当多个JVM客户端,同时在ZooKeeper上创建相同的一个临时节点,因为临时节点路径是保证唯一,只要谁 ...
- Angular如何给动态生成的元素绑定事件
在AngularJS中,操作DOM一般在指令中完成,事件监听机制是在对于已经静态生成的dom绑定事件,而如果在指令中动态生成了DOM节点,动态生成的节点不会被JS事件监听. 举例来说: angular ...
- PHP中用下划线开头的含义
命名的规则 加一个为私有的 加两个一般都是系统默认的,系统预定义的,即所谓:=====================“魔术方法”与“魔术常量”=====================★PHP起止为 ...
- halcon中你不知道的标定板细节
本人文着重阐述以下问题: halcon是否只能使用halcon专用的标定板? halcon标定板如何生成? halcon标定板如何摆放,拍照数量有无限制? halcon是否只能使用halcon专用的标 ...
- (MSSQL)sp_refreshview刷新视图失败及更新Table字段失败的问题解决
在近期工作中遇到一个任务,需要批量更改散布在很多Table中的某字段,同时刷新相关视图,但是在执行脚本时,发现了如下问题 更新字段问题 消息 ,级别 ,状态 ,第 行 对象'View_Simple' ...
- log4cpp总结
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- git重要命令
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- LeetCode OJ:Implement Queue using Stacks(栈实现队列)
比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...
- SpringInAction-- 配置Profile Bean
Profile Bean 使用场景描述: 在开发软件的时候,在数据库方面,往往不是一个库就能解决的,一般分为开发库.测试库.生产库,在这些库设置链接的时候,也会配置其对应的数据. 现有一种方式,就是单 ...
- Composer介绍
Composer介绍 简单的说Composer就是一个PHP的组件包的依赖管理器.早年间PHP其实是有自己的包管理器的,叫PEAR.PEAR就介绍到这里,你只要知道这是一个狗屎一般的管理器就行了.在N ...