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. leetcode-166-分数到小数(用余数判断有没有出现小数的循环体)

    题目描述:   给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: n ...

  2. Found an unexpected Mach-O header code: 0x72613c21

    在按照第三方sdk文档中的Emedded Binaries 中加入了他们的framework,在删除这下面的对应的framework后,问题就得到了解决 发下有个英文的页面也是涉及这个问题的, 描述的 ...

  3. POJ 1287

    #include<iostream> #include<stdio.h> #define MAXN 100 #define inf 1000000000 using names ...

  4. YYYY-mm-dd HH:MM:SS 时间格式

    YYYY-mm-dd HH:MM:SS部分解释 d               月中的某一天.一位数的日期没有前导零.    dd             月中的某一天.一位数的日期有一个前导零.   ...

  5. (转)MySQL日志管理

    MySQL 服务器上一共有六种日志:错误日志,查询日志,慢查询日志,二进制日志,事务日志,中继日志. 原文:https://segmentfault.com/a/1190000003072237 一 ...

  6. Java学习之路(十):异常

    ---恢复内容开始--- 异常的概述和分类 Throwable类是Java语言中所有错误或者异常的超类(也就是说,Java中所有的报错都是继承与Throwable的),也只有当对象是此类或者此类的子类 ...

  7. 弹幕和回到顶部前端web

    弹幕和回到顶部前端web 弹幕 1.效果演示 2.相关代码 <!DOCTYPE html> <html lang="en"> <head> &l ...

  8. Css权重解析

    Css权重解析 关于CSS权重,我们需要一套计算公式来去计算,这个就是 CSS Specificity,我们称为CSS 特性或称非凡性,它是一个衡量CSS值优先级的一个标准 具体规范入如下: spec ...

  9. CentOS6的python2.6升级到python2.7以上版本(可能更详细)

    前言:一些第三方框架为了降低复杂性,新的版本已经开始不支持旧版本的python,比如Django这个web框架1.8版本及以上仅仅只支持python2.7及以上版本(记忆中是这个1.8版本) pip安 ...

  10. ssh 登录进入 docker container

    1.Container安装ssh服务,博主的linux是centos ① 安装ssh sudo yum install openssh-server #安装ssh服务器 service sshd st ...