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 ...
随机推荐
- spring security3.1配置比较纠结的2个问题
转自:http://www.iteye.com/topic/1122629 总论无疑问的,spring security在怎么保护网页应用安全上做得很强很周全,但有些地方还是很差强人意,比如对< ...
- Konva的使用
KonvaJS 快速入门 Konva 是一个 基于 Canvas 开发的 2d js 框架库, 它可以轻松的实现桌面应用和移动应用中的图形交互交互效果. Konva 可以高效的实现动画, 变换, 节点 ...
- 为Pdf批量添加水印
Itext官网下载jar包 /** * PDF工具类 */ public class PdfUtil { public static void main(String[] args) throws E ...
- Python之坐标轴刻度细化、坐标轴设置、标题图例添加
学习python中matplotlib绘图设置坐标轴刻度.文本 http://www.jb51.net/article/134638.htm Python绘图 https://www.cnblogs. ...
- nyoj42——连通图加欧拉(连通图板子)dfs
一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下 ...
- configParse模块
一.配置文件简介 在各种程序里面都有配置文件,为了对配置文件进行操作. python中引入了configParse模块进行操作. 配置数值类型: 配置文件中,我们看到的bool型,整数型,在我们操作的 ...
- Poj 1651 Multiplication Puzzle(区间dp)
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10010 Accepted: ...
- Python中常用的内值方法
1)min(2,4) ## 求最小值 2)max(2,4) ## 求最大值3)sum(range(1,100,2)) ## 求和4)枚举:返回 ...
- vs 添加第三方库lib的两种方法
方法一1.代码: 方法二2.配置: 首先包含头文件 #include “../DuiLib/UIlib.h” 连接器->常规-->附加库目录.即是将lib所在的目录,千万要记得,还要写一处 ...
- eclipse 生成发布的apk (signed zipalign过程)
在发布apk到appstore过程中,上传的apk需要先signed(先生成keystore和key)并zipalign.可按照以下步骤来完成:1. 创建一个keystore和key(右键eclips ...