527. Word Abbreviation
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.
public class Solution {
public List<String> wordsAbbreviation(List<String> dict) {
int len = dict.size();
String[] ans = new String[len];
int[] prefix = new int[len];
for(int i=0;i<len;i++){
prefix[i] = 1;
ans[i] = makeAbbr(dict.get(i),prefix[i]);
}
for(int i=0;i<len;i++){
while(true){
HashSet<Integer> set = new HashSet<Integer>();
for(int j=i+1;j<len;j++){
if(ans[i].equals(ans[j])){
set.add(j);
}
}
if(set.isEmpty()) break;
set.add(i);
for(Integer s:set){
ans[s] = makeAbbr(dict.get(s),++prefix[s]);
}
}
}
return Arrays.asList(ans);
}
public String makeAbbr(String s,int k){
if(k>=s.length()-2){
return s;
}
StringBuilder sb = new StringBuilder();
sb.append(s.substring(0,k));
sb.append(s.length()-k-1);
sb.append(s.charAt(s.length()-1));
return sb.toString();
}
}
//suppose the average of every string could be k,the size of the list could be n,the total run time could be O(n^2*k);the space complexity could be O(n);
527. Word Abbreviation的更多相关文章
- [LeetCode] 527. 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] 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 Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- LeetCode Word Abbreviation
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...
- Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
随机推荐
- proc的妙用
今天在在公司做网络驱动开发测试时,随机包出现收包计数停止的现象,当时怀疑是DMA rx buffer不足导致,想通过对比收发包正常和收发包不正常是DMA相关寄存器的情况. 后跟踪代码,若在收发包里面增 ...
- The Moving Points - HDU - 4717 (模拟退火)
题意 二维空间中有\(n\)个运动的点,每个点有一个初始坐标和速度向量.求出一个时间\(T\),使得此时任意两点之间的最大距离最小.输出\(T\)和最大距离. 题解 模拟退火. 这个题告诉了我,初始步 ...
- 团体程序设计天梯赛-练习集 L2-001 紧急救援 (25 分)
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
- mysql不能登陆
前些天还正常运行的mysql,不知怎么就不能登陆了.错误提示为 :ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (1 ...
- 小知识(h5 js )
1.如果都为NaN但是他们不相等var a=NaN;var b=NaN;a==b //flase2.javascript 是一种脚本语言,可以创建服务器端和客户端的脚本3.javascript 中有两 ...
- 【Linked List Cycle】cpp
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
- [oldboy-django][2深入django]cookies + session
1.1 cookies - 初识cookie a.cookie是保留在浏览器端的键值对 b.服务端可以向客户端写cookie c.客户端每次发送请求,会携带cookie一起发送过去,而且cookie是 ...
- HDU5857 Median 模拟
Median HDU - 5857 There is a sorted sequence A of length n. Give you m queries, each one contains fo ...
- excel模板解析前后设计变化,以及我对此的看法和感受
前言:近期也在做Excel模板的解析工作,目前来说,应该是第三版了.我最开始做的,就是垒鸡窝,虽然考虑了1.0提出的关于excel解析的一些建议和问题(单个模板),但是真的毫无设计可言.就几个工具类, ...
- easyui在datagrid只想选择一条
<table class="" id="jgrid" data-options="fitColumns:true,rownumbers: tru ...