LeetCode Word Abbreviation
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/
题目:
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.
题解:
暴力解法把字典里所有的string都最短缩写,遇到重复的就都加个prefix再检查.
Time Complexity: O(mn). m = dict.size(). n是字典里string的平均长度.
Space: O(m).
AC Java:
- class Solution {
- public List<String> wordsAbbreviation(List<String> dict) {
- int len = dict.size();
- String [] res = new String[len];
- int [] prefix = new int[len];
- for(int i = 0; i<len; i++){
- String abbr = getAbbr(dict.get(i), 0);
- res[i] = abbr;
- }
- for(int i = 0; i<len; i++){
- while(true){
- HashSet<Integer> hs = new HashSet<Integer>();
- for(int j = i+1; j<len; j++){
- if(res[j].equals(res[i])){
- hs.add(j);
- }
- }
- if(hs.isEmpty()){
- break;
- }
- hs.add(i);
- for(int duplicateInd : hs){
- res[duplicateInd] = getAbbr(dict.get(duplicateInd), ++prefix[duplicateInd]);
- }
- }
- }
- return Arrays.asList(res);
- }
- private String getAbbr(String s, int ind){
- int len = s.length();
- if(len-ind <= 3){
- return s;
- }
- return s.substring(0, ind+1) + (len-ind-2) + s.charAt(len-1);
- }
- }
LeetCode Word Abbreviation的更多相关文章
- [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] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode 527---Word Abbreviation
527. Word Abbreviation Given an array of n distinct non-empty strings, you need to generate minimal ...
随机推荐
- 基于事件的 JavaScript 编程:异步与同
JavaScript的优势之一是其如何处理异步代码.异步代码会被放入一个事件队列,等到所有其他代码执行后才进行,而不会阻塞线程.然而,对于初学者来说,书写异步代码可能会比较困难.而在这篇文章里,我将会 ...
- node查询mongo
http://www.cnblogs.com/whoamme/p/3467374.html nosql的数据库的查询:可以分为查询所有,查询一个,条件查询,和表的关联查询.(这个另外在写一个独立的mo ...
- 仅需15分钟,使用OpenCV+Keras轻松破解验证码
https://baijia.baidu.com/s?id=1586732712477979223&wfr=pc&fr=app_lst
- uva 12086 线段树or树状数组练习
题目链接 https://vjudge.net/problem/34215/origin 这个题就是线段树裸题,有两种操作,实现单点更新和区间和的查找即可,这里第一次学习使用树状数组完成. 二者相 ...
- nyoj117——树状数组升级版(树状数组+离散化)
求逆序数 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中 ...
- IOS-程序员和设计师必备的20个CSS工具
程序员和设计师必备的20个CSS工具 CSS工具是现今网站开发人员和设计人员使用的最必要和最重要的工具之一.这是因为这些CSS工具,可以为开发人员和设计人员简化手头的工作,大大减少web开发和设计 ...
- jquery下跨域请求之代码示例
场景描述: 在域A下异步获取B域下的接口: 实现方法: $.ajax({ url : (Q.lottery.serverTimeUrl || 'about:blank'), error : funct ...
- Android_布局属性大全
RelativeLayout 第一类:属性值为true可false android:layout_centerHrizontal 水平居中 android:layout_centerVe ...
- 【网络编程】inet_addr、inet_ntoa、inet_aton、inet_ntop和inet_pton区分
先上一张图 1.把ip地址转化为用于网络传输的二进制数值 int inet_aton(const char *cp, struct in_addr *inp); inet_aton() 转换网络主机地 ...
- Linux输入输出管理
一.系统输入输出的理解 运行一个程序时,需要从某个位置读取输入信息,然后CPU处理,最后将输出 显示在屏幕或文件中:其中,某个位置相当于输入设备,屏幕或文件为输出设备. 标准输入:stdin,默认 ...