原题链接在这里: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.

  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:

  1. Input: ["like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"]
  2. 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.

题解:

暴力解法把字典里所有的string都最短缩写,遇到重复的就都加个prefix再检查.

Time Complexity: O(mn). m = dict.size(). n是字典里string的平均长度.

Space: O(m).

AC Java:

  1. class Solution {
  2. public List<String> wordsAbbreviation(List<String> dict) {
  3. int len = dict.size();
  4. String [] res = new String[len];
  5. int [] prefix = new int[len];
  6.  
  7. for(int i = 0; i<len; i++){
  8. String abbr = getAbbr(dict.get(i), 0);
  9. res[i] = abbr;
  10. }
  11.  
  12. for(int i = 0; i<len; i++){
  13. while(true){
  14. HashSet<Integer> hs = new HashSet<Integer>();
  15. for(int j = i+1; j<len; j++){
  16. if(res[j].equals(res[i])){
  17. hs.add(j);
  18. }
  19. }
  20.  
  21. if(hs.isEmpty()){
  22. break;
  23. }
  24.  
  25. hs.add(i);
  26. for(int duplicateInd : hs){
  27. res[duplicateInd] = getAbbr(dict.get(duplicateInd), ++prefix[duplicateInd]);
  28. }
  29. }
  30. }
  31.  
  32. return Arrays.asList(res);
  33. }
  34.  
  35. private String getAbbr(String s, int ind){
  36. int len = s.length();
  37. if(len-ind <= 3){
  38. return s;
  39. }
  40.  
  41. return s.substring(0, ind+1) + (len-ind-2) + s.charAt(len-1);
  42. }
  43. }

类似Valid Word Abbreviation.

LeetCode Word Abbreviation的更多相关文章

  1. [LeetCode] Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  2. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  3. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  4. [LeetCode] 527. Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  5. Leetcode Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  6. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  7. [Locked] Unique Word Abbreviation

    Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...

  8. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  9. LeetCode 527---Word Abbreviation

    527. Word Abbreviation Given an array of n distinct non-empty strings, you need to generate minimal ...

随机推荐

  1. 基于事件的 JavaScript 编程:异步与同

    JavaScript的优势之一是其如何处理异步代码.异步代码会被放入一个事件队列,等到所有其他代码执行后才进行,而不会阻塞线程.然而,对于初学者来说,书写异步代码可能会比较困难.而在这篇文章里,我将会 ...

  2. node查询mongo

    http://www.cnblogs.com/whoamme/p/3467374.html nosql的数据库的查询:可以分为查询所有,查询一个,条件查询,和表的关联查询.(这个另外在写一个独立的mo ...

  3. 仅需15分钟,使用OpenCV+Keras轻松破解验证码

    https://baijia.baidu.com/s?id=1586732712477979223&wfr=pc&fr=app_lst

  4. uva 12086 线段树or树状数组练习

    题目链接   https://vjudge.net/problem/34215/origin 这个题就是线段树裸题,有两种操作,实现单点更新和区间和的查找即可,这里第一次学习使用树状数组完成. 二者相 ...

  5. nyoj117——树状数组升级版(树状数组+离散化)

    求逆序数 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中 ...

  6. IOS-程序员和设计师必备的20个CSS工具

    程序员和设计师必备的20个CSS工具   CSS工具是现今网站开发人员和设计人员使用的最必要和最重要的工具之一.这是因为这些CSS工具,可以为开发人员和设计人员简化手头的工作,大大减少web开发和设计 ...

  7. jquery下跨域请求之代码示例

    场景描述: 在域A下异步获取B域下的接口: 实现方法: $.ajax({ url : (Q.lottery.serverTimeUrl || 'about:blank'), error : funct ...

  8. Android_布局属性大全

    RelativeLayout 第一类:属性值为true可false android:layout_centerHrizontal        水平居中 android:layout_centerVe ...

  9. 【网络编程】inet_addr、inet_ntoa、inet_aton、inet_ntop和inet_pton区分

    先上一张图 1.把ip地址转化为用于网络传输的二进制数值 int inet_aton(const char *cp, struct in_addr *inp); inet_aton() 转换网络主机地 ...

  10. Linux输入输出管理

      一.系统输入输出的理解 运行一个程序时,需要从某个位置读取输入信息,然后CPU处理,最后将输出 显示在屏幕或文件中:其中,某个位置相当于输入设备,屏幕或文件为输出设备. 标准输入:stdin,默认 ...