原题链接在这里: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. Android显示框架:自定义View实践之绘制篇

    文章目录 一 View 二 Paint 2.1 颜色处理 2.2 文字处理 2.3 特殊处理 三 Canvas 3.1 界面绘制 3.2 范围裁切 3.3 集合变换 四 Path 4.1 添加图形 4 ...

  2. ctci1.3

    ; i < len; i++){         if(str0[i] != str1[i])             return false;     }          return t ...

  3. Sql Server 2008 清理日志文件

    Use LingZhong Select NAME,size From sys.database_files ALTER DATABASE LingZhong SET RECOVERY SIMPLE ...

  4. [转]通过rsync+inotify-tools+ssh实现触发式远程实时同步

    文件的同步镜像在很多地方都需要用到,因此rsync这款免费软件得到了广泛的应用,包括在Windows平台上,都已经有了支持rsync的“cwRsyncServer”. 但是,我们一般都是通过结合cro ...

  5. Java集合详解6:TreeMap和红黑树

    Java集合详解6:TreeMap和红黑树 初识TreeMap 之前的文章讲解了两种Map,分别是HashMap与LinkedHashMap,它们保证了以O(1)的时间复杂度进行增.删.改.查,从存储 ...

  6. HDU 4696 Answers (脑补+数形结合)

    题意 给一个图,每个点的出度为1,每个点的权值为1或者2.给Q个询问,问是否能找到一条路径的权值和M. 思路 由于每个点的出度为1,所以必然存在环.又因为c[i]只能取1或者2,可以组成任意值,所以只 ...

  7. 实现Callable接口创建线程

    创建执行线程有四种方式: 实现implements接口创建线程 继承Thread类创建线程 实现Callable接口,通过FutureTask包装器来创建线程 使用线程池创建线程 下面介绍通过实现Ca ...

  8. 报错:Type mismatch: cannot convert from Object to Car

    问题描述: 一个非常简单的spring项目,用静态工厂方法配置bean实例.项目的目录结构如下: 代码如下: Car.java package com.tt.spring.beans.factory; ...

  9. live555 中的socket的任务调度分析

    1.添加一个socket任务 envir().taskScheduler().setBackgroundHandling(socketNum, SOCKET_WRITABLE|SOCKET_EXCEP ...

  10. Miscosoft Visual Studio项目guid取值

    There isn't an easy way to change the type of a project in Visual Studio project once it is created; ...