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 ...
随机推荐
- 【数组】Missing Number
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- Android 开发工具类 20_DOMPersonService
xml 文件 <?xml version="1.0" encoding="UTF-8"?> <persons> <person i ...
- 如何虚拟机里安装Win10操作系统
不多说,直接上干货! Windows Server 2003.2008.2012系统的安装 推荐网址:打开MSDN网站(http://msdn.itellyou.cn ) 关于给电脑换系统,很多人会花 ...
- Python -- Gui编程 -- Win32API的使用
消息框 messageBox.py import win32api, win32con win32api.MessageBox(0, 'Hello World!', 'Come Here', win3 ...
- 回退Ubuntu记录
前言 由于Ubuntu18经常出错,因而决定回退Ubuntu16,下面是记录回退问题及美化,以便以后需要. 问题总结 磁盘挂载 挂载其他磁盘分区时,提示错误"Metadata kept in ...
- Jfinal本地eclipse+tomcat运行项目时候遇到错误Exception starting filter
今天想在本地eclipse上启动tomcat让项目在本地运行,但是老是报错类找不到异常. 也可能报其它错误,大概都是classNotFoundException. 九月 19, 2018 5:42:2 ...
- android学习-LocationManager(一)-定位方式原理解析
参考资源:android 4种定位原理及实现——1 android使用不同的方法为应用提供位置信息. 定位的方式有三种:GPS地位(A-GPSAssistedGPS:辅助全球卫星定位系统,或者是同步G ...
- R语言之数据处理常用包
dplyr包是Hadley Wickham的新作,主要用于数据清洗和整理,该包专注dataframe数据格式,从而大幅提高了数据处理速度,并且提供了与其它数据库的接口:tidyr包的作者是Hadley ...
- Cloudera Impala源码分析: SimpleScheduler调度策略详解包括作用、接口及实现等
问题导读:1.Scheduler任务中Distributed Plan.Scan Range是什么?2.Scheduler基本接口有哪些?3.QuerySchedule这个类如何理解?4.Simple ...
- Nodejs学习笔记(十七)—浮点运算decimal.js
前言 开发过程中免不了有浮点运算,JavaScript浮点运算的精度问题会带来一些困扰 JavaScript 只有一种数字类型 ( Number ) JavaScript采用 IEEE 754 标准双 ...