zoukankan      html  css  js  c++  java
  • 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.

    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.

    题解:

    暴力解法把字典里所有的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.

  • 相关阅读:
    dev DEV控件:gridControl常用属性设置
    C# ListView用法详解
    LeetCode 22_ 括号生成
    LeetCode 198_ 打家劫舍
    LeetCode 46_ 全排列
    LeetCode 121_ 买卖股票的最佳时机
    LeetCode 70_ 爬楼梯
    LeetCode 53_ 最大子序和
    LeetCode 326_ 3的幂
    LeetCode 204_ 计数质数
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/8329260.html
Copyright © 2011-2022 走看看