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 ...
随机推荐
- led,key通用IO的端口
1 注意通用IO端口, GPBCON 只能控制一个GPBDAT位(对应的位),而GPBUP可以使能GPBCON.
- java使用poi实现excel表格生成
通过使用poi技术生成Excel,使用反射技术实现自动映射列表的数据. ExportTableUtil.java public class ExportTableUtil { /** * * @Des ...
- java:历史回顾
1.String和StringBuffer区别 2.Runtime和System类,包括对象垃圾收集 Rumtime.gc() System.gc() 调用的其实就是Runtime的gc回收 3.da ...
- Metasploit 使用简介
Metasploit Framework 是非常优秀的开源渗透测试框架,像我这样的菜鸟刚刚听说,于是花时间好好研究了一下,整理了一下学习笔记,贴出来和大家一起交流.第一次写文章又不足的地方大家多多指点 ...
- IOS-pch文件配置
--到Xcode7都可以这么解决.亲测. 发现一个好东西.就是这个.pch文件.我的理解是他里面存放了我们在各个controller里面需要的头文件,那这样一来,就免去了在不同的ViewControl ...
- Factorialize a Number
计算一个整数的阶乘 如果用字母n来代表一个整数,阶乘代表着所有小于或等于n的整数的乘积. 阶乘通常简写成 n! 例如: 5! = 1 * 2 * 3 * 4 * 5 = 120 当你完成不了挑战的时候 ...
- Disruptor快速入门
在JDK的多线程与并发库一文中, 提到了BlockingQueue实现了生产者-消费者模型 BlockingQueue是基于锁实现的, 而锁的效率通常较低. 有没有使用CAS机制实现的生产者-消费者? ...
- Poj 2955 brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7795 Accepted: 4136 Descript ...
- 多线程相关(pthread 、NSThread 、GCD、NSOperation)
进程 进程是指在系统中正在运行的一个应用程序 线程 1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程) 1个线程中任务的执行是串行的(执行完上一个才能执行下一个) 多线程 1个进程中可以 ...
- windows的虚拟磁盘(vhd,vhdx)使用
以前一直使用u盘或者移动硬盘接上usb直接拷贝文件,发觉速度一般.而且一般只有一个盘,分类也很不方便. 后来发现windows的虚拟磁盘可以解决我的问题... 经过一段时间的使用后发觉使用虚拟磁盘的方 ...