原题链接在这里: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.

  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.

题解:

暴力解法把字典里所有的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);
}
}

类似Valid Word Abbreviation.

LeetCode Word Abbreviation的更多相关文章

  1. [LeetCode] Word Abbreviation 单词缩写

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

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

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

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

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

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

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

  5. Leetcode Unique Word Abbreviation

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

  6. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  7. [Locked] Unique Word Abbreviation

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

  8. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  9. LeetCode 527---Word Abbreviation

    527. Word Abbreviation Given an array of n distinct non-empty strings, you need to generate minimal ...

随机推荐

  1. 轻量级 HTTP(s) 代理 TinyProxy

      J CentOS 下安装 TinyProxy yum install -y tinyproxy 启动.停止.重启 # 启动service tinyproxy start# 停止service ti ...

  2. xshell5使用ssh连接阿里云服务器

    这里有两种方式,一种是在阿里云的控制台里面进行,另一种是在Xshell里面生成密钥. 阿里云控制台密钥对 点击右上方的创建密钥对 在阿里云里面生成较为简单,点击该页面右上方的“创建密钥对”,在另一个页 ...

  3. 初识HTML和CSS

    HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据库获取数据,然后替换到html文件的指定位置(W ...

  4. MongoDB3.xxx 用户创建

    启动MongoDB前需要关闭配置文件中的auth选项,否则不能创建用户 首先创建用户管理用户 use admin db.createUser({user:'admin',pwd:'123456', r ...

  5. Android------第一次启动出现白屏或者黑屏

    APP开发中,第一次运行启动app时,会出现一会儿的黑屏或者白屏才进入Activity的界面显示. 当打开一个Activity时,如果这个Activity所属Application还没有在运行, 系统 ...

  6. C# - Generics泛型,一图话c#泛型

    一.一篇好文 https://www.cnblogs.com/yueyue184/p/5032156.html 二.一幅好图

  7. es6环境中,export与import使用方法

    前言 参考自阮一峰大神的教程:http://es6.ruanyifeng.com/?search=export&x=6&y=5#docs/module#export-命令 声明:如有问 ...

  8. bzoj3623

    题解: 刚看到题目,还以为是2-sat 可是似乎不对啊... 然后就只能爆搜了 看了网上的题解,woc还真是报搜 然后就ac了 当然爆搜还要随机化 代码: #include<bits/stdc+ ...

  9. XML方式实现Spring的AOP

    1.编写切面类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.fz.an ...

  10. Hibernate主键生成策略详解

    转载自:http://blog.csdn.net/wanghuan203/article/details/7562395 hibernate提供的主键生成策略,使我们可以在实体类的映射xml文件中设定 ...