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. Vim编辑器与shell脚本

      目录                                                          Vim文本编辑器 Shell脚本 Shell编程变量 流程控制语句 计划任务 ...

  2. leetcode-917-仅仅反转字母

    题目描述: 给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转. 示例 1: 输入:"ab-cd" 输出:"dc-b ...

  3. Andrew Ng机器学习第五章——多变量线性回归

    一.多变量线性回归的技巧之一——特征缩放 1.为什么要使用特征缩放? 特征缩放用来确保特征值在相似的范围之内. 设想这样一种情况(房价预测),两个特征值分别是房子的大小和卧室的数量.每个特征值所处的范 ...

  4. Swift循环遍历集合方法

      第一种方式:for-in循环 OC延续了C语言的for循环,在Swift中被彻底改造,我们无法再使用传统形式的for循环了 遍历数组和字典: //遍历数组 let iosArray = [&quo ...

  5. 【原创】SSRS (SQL Serve Reporting Service) 访问权限的问题

    问题:The permissions granted to user 'TOUCHPOINTMED\sshi' are insufficient for performing this operati ...

  6. 使用EntityFrameworkCore 连接 MySql

    上篇文章介绍了如何在dotnetcore下使用Dapper连接MySql,这里再介绍使用使用EntityFrameworkCore 连接 MySql. 新建控制台项目,安装下面两个nuget包: In ...

  7. 【树】Lowest Common Ancestor of a Binary Tree(递归)

    题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...

  8. 关于Spring配置的一些东西

    Spring 配置的三种方式:JAVA配置,注解配置,和XML的配置 注解配置: @Service:标识服务层(业务层)组件 @Component:基本注解, 标识了一个受 Spring 管理的组件( ...

  9. Go 单元测试、基准测试、并发基准测试

    一.单元测试 要开始一个单元测试,需要准备一个 go 源码文件,在命名文件时需要让文件必须以_test结尾. 单元测试源码文件可以由多个测试用例组成,每个测试用例函数需要以Test为前缀,例如: fu ...

  10. Inno Setup设置在安装Finished页面,点击finish后打开网页

    在安装的最后一个页面FinishPage中点击Finished然后打开一个网页 这个功能貌似很简单,不就是在点击finish按钮给它绑定事件,问题立马解决. 在普通的桌面应用程序开发中的确是这样做的, ...