527. Word Abbreviation

  • 方法1:
    • Complexity

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:

  1. Both n and the length of each word will not exceed 400.
  2. The length of each word is greater than 1.
    3l.The words consist of lowercase English letters only.
    The return answers should be in the same order as the original array.

方法1:

grandyang: https://www.cnblogs.com/grandyang/p/6818742.html

思路:

所谓的greedy,我们先尝试用一个prefix来压缩所有单词,然后开始检查是否有重复,如果有就进一步解压缩,直到没有collision的一颗。那么怎么实现呢,首先要用一个新的vector<\string>来记录当前压缩的每个单词结果。对于每一个压缩,开始向后面所有的单词发起遍历,如果发现一样的缩写,暂时将所有一样的index存起来,在遍历结束后对所有对应单词进一步压缩。那么又会需要一个vector<\int>来对每一个单词记录迄今为止压缩的prefix位置,每一次进一步压缩要所有重复一起++。

Complexity

Time Complexity: O(C^2) where CC is the number of characters across all words in the given array
Space Complexity: O©

class Solution {public:vector<string> wordsAbbreviation(vector<string>& dict) {int n = dict.size();vector<int> prefix(n, 1);vector<string> res;for (string word: dict) {if (word.size() <= 3) res.push_back(word);else {res.push_back(*word.begin() + to_string(word.size() - 2) + *word.rbegin());}}for (int i = 0; i < n; i++) {while (true) {vector<int> dup;for (int j = i + 1; j < n; j++) {if (res[i] == res[j]) dup.push_back(j);}if (dup.empty()) break;dup.push_back(i);for (int k: dup) res[k] = abbrev(dict[k], ++prefix[k]);}}return res;}string abbrev(string & word, int pre) {return (pre >= word.size() - 2) ? word : (word.substr(0, pre) + to_string(word.size() - pre - 1) + word.back());}
};

527. Word Abbreviation相关推荐

  1. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  2. 288. Unique Word Abbreviation

    288. Unique Word Abbreviation 方法1: 方法2: 易错点 An abbreviation of a word follows the form . Below are s ...

  3. 408. Valid Word Abbreviation

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

  4. 288. Unique Word Abbreviation

    题目: An abbreviation of a word follows the form <first letter><number><last letter> ...

  5. [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  6. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  7. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  8. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

  9. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

  10. 这篇文献总结了常见的中式英语写法,来看看有没有中枪?

    The Most Common Habits from more than 200  English Papers written by Graduate Chinese Engineering St ...

最新文章

  1. jq 获取当时时间的到秒_空军飞行员忆战机被鸟撞后的37秒
  2. python使用笔记:pyperclip模块安装和使用
  3. 多线程:无锁、偏向锁、轻量锁、重量级锁
  4. 如何降低90%Java垃圾回收时间?以阿里HBase的GC优化实践为例
  5. 搭建一个基于http的yum服务器
  6. C# winform 窗体怎么隐藏标题栏,不显示标题栏
  7. python 隐马尔科夫_机器学习算法之——隐马尔可夫(Hidden Markov ModelsHMM)原理及Python实现...
  8. java struts 框架_java中struts 框架的实现
  9. 异常是catch还是throws的简单原则
  10. 百度地图Key的设置方法
  11. Oracle中的AWR,全称为Automatic Workload Repository
  12. android端与windows端通信中文乱码问题
  13. 不用掉一根头发!用 Flutter + Dart 快速构建一款绝美移动 App
  14. 算法设计与分析基础_学习笔记
  15. 英雄无敌3_经典地图_下载
  16. 基于JavaWeb网上商城(以卖书为主)
  17. OutLook 2013 添加 USC gmail 邮箱 解决国内USC邮箱打不开
  18. rows between unbounded preceding and current row等,unbounded ,preceding ,following,current row含义详解
  19. Xshell 下载地址
  20. 重磅!吴恩达深度学习又开新课啦!

热门文章

  1. 全球及中国生物制药产业盈利现状及竞争格局展望报告2021-2027年
  2. C++与UMG的交互
  3. 关于Gateway实现JWT登陆拦截过滤器
  4. vue 项目实现短信发送
  5. Android 仿自如APP裸眼3D效果
  6. 海康威视多监控集成到同一页面
  7. ReadHub源码阅读笔记(二)dagger+MVP
  8. 人这一辈子,渡你的只有两个人
  9. 【小程序】扫码预览时不显示图片
  10. English - because of,due to ,thanks to ,owing to ,as a result of ,on account of解析