[LintCode] Word Abbreviation

本文介绍了一种用于生成字符串数组中每个单词最小可能缩写的算法。该算法确保最终的缩写不映射到多个原始单词,并提供了实现代码示例。

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.
 Notice
  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.
Example
Given dict = ["like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"]
return ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]


This problem is not hard not algorithmic wise, but implementation wise. To tackle this kind of problem,
going through a good example as simulation is sufficient enough to get the following working algorithm.

1. Create an arraylist that stores not-uniquely mapped string's index.
2. Initialize nonAbbrLen = 2, according to rule 3.
3. As long as there is still not-uniquely mapped string, repeat the following.

  a.For each not-uniquely mapped string str, get its next level's abbreviation abbr and
store them in (abbr, str's index in dict[]).
   b.After going through all the strings, add all uniquely mapped strings from 
the hash map to the final result.

c.For all not uniquely mapped strings, add them to a new array list and assign
this list to unprocessedIndex.

d.Increase the next abbreviation level by 1 to further distinguish strings that
currently share the same abbreviation.

e. Repeat until unprocessdIndex is empty.

 1 public class Solution {
 2     public String[] wordsAbbreviation(String[] dict) {
 3         if(dict == null || dict.length == 0){
 4             return new String[0];
 5         }
 6         int n = dict.length;
 7         String[] res = new String[n];
 8         List<Integer> unprocessedIndex = new ArrayList<Integer>();
 9         for(int i = 0; i < n; i++){
10             unprocessedIndex.add(i);
11         }
12         int nonAbbrLen = 2;
13         while(unprocessedIndex.size() != 0){
14             HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
15             for(int i : unprocessedIndex){
16                 String curr = dict[i];
17                 String abbr = curr.length() <= (nonAbbrLen + 1) ?
18                               curr : curr.substring(0, nonAbbrLen - 1) + (curr.length() - nonAbbrLen)
19                               + curr.charAt(curr.length() - 1);
20                 if(map.containsKey(abbr)){
21                     map.get(abbr).add(i);
22                 }
23                 else{
24                     ArrayList<Integer> list = new ArrayList<Integer>();
25                     list.add(i);
26                     map.put(abbr, list);
27                 }
28             }
29             List<Integer> next = new ArrayList<Integer>();
30             for(String word : map.keySet()){
31                 if(map.get(word).size() == 1){
32                     for(int idx : map.get(word)){
33                         res[idx] = word;
34                     }
35                 }
36                 else{
37                     for(int idx : map.get(word)){
38                         next.add(idx);
39                     }
40                 }
41             }
42             unprocessedIndex = next;
43             nonAbbrLen++;
44         }
45         return res;
46     }
47 }

 

The key point of the above solution is to use hashmap to determine if a certain abbreviation level can uniquely map to 

only one word. If a map key points to a list of size 1, then we know this key uniquely maps to one word.

 

 

Related Problems 

Check Word Abbreviation

转载于:https://www.cnblogs.com/lz87/p/6948817.html

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值