LeetCode 527---Word Abbreviation
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.
- Begin with the first character and then the number of characters abbreviated, which followed by the last character.
- 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.
- 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:
- Both n and the length of each word will not exceed 400.
- The length of each word is greater than 1.
- The words consist of lowercase English letters only.
- 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的更多相关文章
- [LeetCode] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- Leetcode: Valid Word Abbreviation
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- 527. Word Abbreviation
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- [LeetCode] Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- LeetCode Word Abbreviation
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...
随机推荐
- OS之进程管理 --- 死锁
什么是死锁 在正常操作模式下,进程按如下顺序来使用资源: 申请:进程请求资源 使用:进程对资源进行操作 释放:进程释放资源 当一组进程中的每一个进程度在等待一个事件,而这事件只能有一组进程的另一个进程 ...
- 简单选择排序实现(Java)
简单选择排序实现 简单选择排序就是通过n-i次关键字之间的比较,从n-i+1个记录中选出关键字最小的记录,并和第i (1 ≤ i ≤ n)个记录交换. 代码实现: public class Selec ...
- 无apk,怎么获取app的activity
在做app自动化测试之前,有个前提条件,就是要获取当前app的包名和当前活动的activity.如果有提供了.apk,就可以直接通过adb命令获取到包名和欢迎页面:有种软件是手机自带的的,我们不知道a ...
- 【转】Unity Animator卡顿研究
Unity Animator卡顿研究 发表于2017-07-26 点赞3 评论3 分享 分享到 2.3k浏览 想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学院游戏程序行业精英群 ...
- LODOP内嵌挡住浏览器的div弹出层
首先,做一个简单的div弹出层用于测试,该弹出层的介绍可查看本博客另一篇博文:[JS新手教程]浏览器弹出div层 然后加入LODOP内嵌,LODOP可以内嵌,C-LODOP不能内嵌,可以在IE等浏览器 ...
- android图片压缩总结
一.bitmap 图片格式介绍 android中图片是以bitmap形式存在的,那么bitmap所占内存,直接影响到了应用所占内存大小,首先要知道bitmap所占内存大小计算方式: bitmap内存大 ...
- Postman—命令执行脚本及生成报告
前言 前面的应用中,都是在postman图形界面工具里面进行测试,但是有时候我们需要把测试脚本集成到CI平台,或者在非图形界面的系统环境下进行测试,那么我们该如果处理呢 通过newman来执行post ...
- PHP操作MySQL数据库步骤:
简单的PHP操作数据库步骤: 1.连接数据库 $con = mysql_connect('ip','username','password'); 2.选择数据库 mysql_select_ ...
- Spanner:谷歌新一代全球部署的列式数据库
Spanner 是一个可扩展的.全球分布式的数据库,提供分布式ACID. 架构 universe:一个部署的实例成为universe,目前谷歌有3个,分别为开发/测试/线上 Zone:一个数据中心,相 ...
- The best career advice I’ve received --转载
http://www.nczonline.net/blog/2013/10/15/the-best-career-advice-ive-received/ The best career advice ...