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.

  • 相关阅读:
    小程序---云开发----云函数
    小程序的基本概念-生命周期(组件 wxml)
    小程序的基本概念
    vue登录功能和将商品添加至购物车实现
    vue脚手架创建项目
    node.js评论列表和添加购物车数据库表创建
    学习脚手架--组件之间跳转与参数(组件之间参数)
    node.js 需要注意知识点
    如何查询小程序官方手册
    vue ui九宫格、底部导航、新闻列表、跨域访问
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/8329260.html
Copyright © 2011-2022 走看看