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.

  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.

算法分析:

构造HashMap<String,ArrayList>abbre2Word,以每个字符串的Abbreviation做键,向该键下的ArrayList内添加映射到该键的Word。对于每个Abbreviation,如果其映射的ArrayList的size()为1,则该abbreviation为unique的,将该(Word,Abbreviation)添加到另一个 HashMap<String,String> word2Abbre,该映射以 Word 做键,以Abbreviation 做值;如果abbre2Word中的Abbreviation对应的ArrayList的 size() 大于1,则以此ArrayList做参数递归调用函数来重新生成Abbreviation,并且调用函数的时候传入 prefix 长度参数,该参数比上一次调用增加1。

Java算法实现:

public class Solution {
public List<String> wordsAbbreviation(List<String> dict) {
Map<String, String>map=new HashMap<>();
WordMap2Abbreviation(map, 0, dict);
List<String>result=new ArrayList<>();
int size=dict.size();
for(int i=0;i<size;i++){
result.add(map.get(dict.get(i)));//调整map中Abbreviation的顺序,使result中的Abbreviation与dict中同一位置上的word相对应
}
return result;
} public String getAbbreviation(String word,int fromIndex){//fromIndex表示从word的第几个字符开始生成缩写词
int len=word.length();
if(len-fromIndex<=3){//3个及以下的字符没有缩写的必要
return word;
}
else{
return word.substring(0, fromIndex+1)+String.valueOf(len-fromIndex-2)+word.charAt(len-1);
}
} public void WordMap2Abbreviation(Map<String, String>map,int fromIndex,List<String>dict){
Map<String,ArrayList<String>>abbre2Word=new HashMap<>();//以abbreviation做键,value为Abbreviation相同的word组成的ArrayList<String>
for(String word:dict){
String abbre=getAbbreviation(word, fromIndex);
if(abbre2Word.containsKey(abbre)){
abbre2Word.get(abbre).add(word);
}
else{
ArrayList<String>list=new ArrayList<>();
list.add(word);
abbre2Word.put(abbre, list);
}
}
for(String abbre:abbre2Word.keySet()){
ArrayList<String>words=abbre2Word.get(abbre);
if(words.size()==1){//说明该Abbreviation是unique的
map.put(words.get(0), abbre);
}
else{
WordMap2Abbreviation(map, fromIndex+1, words);//对这些Abbreviation相同的word递归调用函数
}
}
}
}

LeetCode 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. [LeetCode] Valid Word Abbreviation 验证单词缩写

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

  3. Leetcode Unique Word Abbreviation

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

  4. [LeetCode] Unique Word Abbreviation 独特的单词缩写

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

  5. Leetcode: Valid Word Abbreviation

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

  6. 527. Word Abbreviation

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

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

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

  8. [LeetCode] Word Abbreviation 单词缩写

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

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

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

  10. LeetCode Word Abbreviation

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

随机推荐

  1. js之global 对象 方法

    global 作为js的全局对象,但其是无法直接访问的,但是在浏览器中浏览器是将这个对象当做是window对象的一部分,即Date 等Global的属性使用window.Date 可访问到 1.url ...

  2. 使用findbugs为自己的代码review

    转自:http://blog.lichengwu.cn/java/2013/11/24/use-findbugs-code-review/ 介绍 Findbugs是一个代码静态分析工具,用来找出Jav ...

  3. FlowPortal-BPM——功能:判断数据库表中字段是否重复并阻止提交或保存

    一.JS添加代码,判断是否有OnSubmit事件 文件位置:YZSoft/Forms/src/Validator.js //=====判断是否有OnSubmit事件===== if (typeof ( ...

  4. spring中的context:include-filter和context:exclude-filter的区别

    在Spring 的配置文件中有: <context:component-scan base-package="njupt.dao,njupt.service"> < ...

  5. VSTO学习(五)——创建Word解决方案

    一.引言 在上一个专题中主要为大家介绍如何自定义我们的Excel 界面的,然而在这个专题中,我将为大家介绍如何用VSTO来创建Word项目,对于Word的VSTO开发和Excel的开发很类似,你同样也 ...

  6. 剑指offer五十三之表示数值的字符串

    一.题目 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3.1 ...

  7. 利用Windows资源监视器解决文件夹无法改名无法删除问题

    在win7等Windows系统操作文件夹更名.删除时经常会报错,操作无法完成,balabala 这个时候仅凭在用的软件去一个一个找是很难的,即便软件全关了,还有后台进程,,,奔溃吧 好了,现在我们有了 ...

  8. 漫谈NIO(2)之Java的NIO

    1.前言 上章提到过Java的NIO采取的是多路IO复用模式,其衍生出来的模型就是Reactor模型.多路IO复用有两种方式,一种是select/poll,另一种是epoll.在windows系统上使 ...

  9. Java之IO(七)ObjectInputStream和ObjectOutputStream

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7003536.html 1.前言 本章介绍Java字节流中重要的成员,对象流ObjectInputStream和O ...

  10. matplotlib基本使用(矩形图、饼图、热力图、3D图)

    使用matplotlib画简单的图形: #-*- coding:utf-8 -*- from numpy.random import randn import matplotlib.pyplot as ...