原题链接在这里: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. Nordic官方网络资源介绍(官网/devzone/GitHub)

    本文将介绍Nordic官方网络资源,包括Nordic官网,开发者论坛(devzone),以及Nordic在GitHub上的共享资源. 1. Nordic官网(产品/SDK/工具/文档库) Nordic ...

  2. 【Python】常用排序算法的python实现和性能分析

    作者:waterxi 原文链接 背景 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试题整 ...

  3. CSS布局框架 960GS 表单排版示例

  4. Android6.0------权限申请管理(单个权限和多个权限申请)

    Android开发时,到6.0系统上之后,有的权限就得申请才能用了. Android将权限分为正常权限 和 危险权限 Android系统权限分为几个保护级别.需要了解的两个最重要保护级别是 正常权限  ...

  5. 从工程角度看C++观察者模式中的接口是否需要提供默认的实现

    在C++中,我们会经常用到观察者模式(回调模式,Delegate模式等,意思都一样),比如当Source中的某个参数发生了变化时,我们通过观察者模式进行回调通知,下面是一个例子: class Sour ...

  6. Mybatis Generator 扩展

    目标 修改Model的名称 修改Dao的名称 配置文件 context.targetRuntime 替换为自定义的类型 原理在:org.mybatis.generator.internal.Objec ...

  7. MYSQL中的日期转换

    MYSQL中的日期转换 网址: http://www.eygle.com/digest/2006/09/mysql_date_convert.html 对于每个类型拥有的值范围以及并且指定日期何时间值 ...

  8. Mit-Scheme 安装小记

    Win10 到 http://www.gnu.org/software/mit-scheme/ 下载对应平台的安装包,我下载的是windows 版本 安装到本地后只出现一个快捷方式MIT-GNU Sc ...

  9. 决定整理一下canvas的基础学习

    好久没有用过canvas,都要忘完了.还是决定复习一下以前的笔记,以及整理一下笔记,以后好查阅

  10. Alpha冲刺一 (2/10)

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9960487.html 作业博客:https://edu.cnblogs.com/campus/ ...