Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below.

  1. Begin with the first character and then the number of characters abbreviated, which followed by the last character.
  2. 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.
  3. 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:

  1. Both n and the length of each word will not exceed 400.
  2. The length of each word is greater than 1.
  3. The words consist of lowercase English letters only.
  4. 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的更多相关文章

  1. [LeetCode] 527. Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  2. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  3. [LeetCode] Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  4. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  5. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  6. Leetcode Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  7. [Locked] Unique Word Abbreviation

    Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...

  8. LeetCode Word Abbreviation

    原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...

  9. Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

随机推荐

  1. 09.1.VUE学习之watch监听属性变化实现类百度搜索栏功能ajax异步请求数据,返回数组

    09.1html里 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  2. [USACO]奶牛抗议(DP+树状数组+离散化)

    Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组 ...

  3. 牛客练习赛42 A 字符串

    题目描述 给定两个等长的由小写字母构成的串 A,BA,B,其中 |A|=|B|=n|A|=|B|=n. 现在你需要求出一个子区间 [l,r][l,r] 使得 LCP(A[l,r],B[l,r])×LC ...

  4. DiyCode开源项目 AboutActivity分析

    1.首先看一下效果 这是手机上显示的效果: 1.1首先是一个标题栏,左侧一个左箭头,然后一个图标. 1.2然后下方是一个可以滑动的页面. 1.3分成了7个部分. 1.4DiyCode的图标. 1.5然 ...

  5. [原]sencha touch之carousel

    carousel组件是个非常不错的东东,自带可滑动的效果,效果如下图 上面部分可以左右滑动,下面部分可以上下滑动,效果还是不错的,app程序中很有用的布局 代码如下: Ext.application( ...

  6. Marketing learning-1

    Today we start to learn something about marketing together.Sometimes i just propose a question,and i ...

  7. Leetcode 507.完美数

    完美数 对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为"完美数". 给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False 示例: ...

  8. bat 处理adb脚本

    @echo off REM Funtion: 测试parsermode 接口CdxParserGetMediaInfo 和CdxParserRead REM Code by lzp 2017-05-0 ...

  9. POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈

    POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ...

  10. caffe-dnnh实验

    下面是我在做基于深度哈希的大规模图像检索中的一个实验,相关文档介绍给大家,具体内容查看提供的相关链接,总结的很到位了,我就不再赘述. 实践cvpr2015年的深度哈希图像检索论文:Simultaneo ...