[LeetCode] Word Abbreviation 单词缩写
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:
- Both n and the length of each word will not exceed 400.
- The length of each word is greater than 1.
- The words consist of lowercase English letters only.
- The return answers should be in the same order as the original array.
这道题让我们求单词的缩写形式,就是首尾字母加上中间字符的个数组成的新字符串,但是要求是不能有重复的缩写字符串,而且说明如果缩写字符串的长度并没有减小的话就保留原来的字符串,比如 god,缩写成 g1d 也没啥用,所以仍是 god。博主刚开始在研究题目中给的例子的时候有些疑惑,虽然知道 internal 和 interval 的缩写形式都是 i6l,会冲突,博主刚开始不明白的是,为什么不能一个是 i6l,一个是 in5l,这样不就不冲突了么,而题目中的缩写形式居然都是原字符串。后来才搞清楚题目原来是说只要有冲突的都不能用,而 internal 和 interval 是典型的死杠上的一对,i6l,in5l,int4l,inte3l,inter2l,统统冲突,而再往后的缩写长度就和原字符串一样了,所以二者就都保留了原样。理解了题意就好办了,由于每个单词的缩写形式中数字前面的字母个数不一定相同,所以用一个 pre 数组来记录每个单词缩写形式开头字母的长度,初始化都为1,然后先求出所有单词 pre 为1的缩写形式,再来进行冲突处理。遍历每一个缩写字符串,进行 while 循环,新建一个 HashSet,然后遍历其他所有字符串,所有发现冲突字符串,就把冲突字符串的坐标存入 HashSet 中,如果没有冲突,那么 HashSet 为空,直接 break 掉,如果有冲突,那么还要把当前遍历的位置i加入 HashSet 中,然后遍历 HashSet 中所有的位置,对其调用缩写函数,此时 pre 对应的值自增1,直到没有冲突存在为止,参见代码如下:
class Solution {
public:
vector<string> wordsAbbreviation(vector<string>& dict) {
int n = dict.size();
vector<string> res(n);
vector<int> pre(n, );
for (int i = ; i < n; ++i) {
res[i] = abbreviate(dict[i], pre[i]);
}
for (int i = ; i < n; ++i) {
while (true) {
unordered_set<int> st;
for (int j = i + ; j < n; ++j) {
if (res[j] == res[i]) st.insert(j);
}
if (st.empty()) break;
st.insert(i);
for (auto a : st) {
res[a] = abbreviate(dict[a], ++pre[a]);
}
}
}
return res;
}
string abbreviate(string s, int k) {
return (k >= (int)s.size() - ) ? s : s.substr(, k) + to_string((int)s.size() - k - ) + s.back();
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/527
类似题目:
Minimum Unique Word Abbreviation
参考资料:
https://leetcode.com/problems/word-abbreviation/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Word Abbreviation 单词缩写的更多相关文章
- [LeetCode] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- LeetCode Word Abbreviation
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LeetCode] Word Frequency 单词频率
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- [Leetcode] word search 单词查询
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [Leetcode] word ladder 单词阶梯
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
随机推荐
- 爬虫(scrapy中调试文件)
在项目setting同级目录下创建py文件,代码如下: from scrapy.cmdline import execute import sys import os sys.path.append( ...
- 基于python的接口自动化测试+ddt数据驱动
在测试接口时,一个接口会先写好测试用例,这个用例主要针对功能,传入参时考虑到各种场景,正常的,异常的,如:参数缺省,参数传一个六位数字写用例时考虑边界情况等. 一个接口设计用例时有可能会十几条到几十条 ...
- 初始配置JDK
什么是java? java是一门编程语言 编程语言有很多种 你比如 C语言 等等 为什么学习java呢! 因为你要和计算机交互 当然了你用汉语跟她说她听不懂 所以你要学习编程语言 那么额咱们的ja ...
- Spring学习笔记(1)
激发pojo的潜能,不会让pojo类继承实现或导入与Spring API相关的任何东西 那么spring怎么装配pojo呢-------->依赖注入(最大好处:松耦合) 耦合具有两面性: 一方面 ...
- C# 7.0 观察者模式 以及 delegate 和 event
观察者模式 这里面综合了几本书的资料. 需求 有这么个项目: 需求是这样的: 一个气象站, 有三个传感器(温度, 湿度, 气压), 有一个WeatherData对象, 它能从气象站获得这三个数据. 还 ...
- 2017级C语言教学总结
一个学期下来,对于这门课教学还是感受挺多,多个教学平台辅助,确实和我前10年的教学方式区别很多,也辛苦很多. 一.课堂教学方面 1.课堂派预习作业 主要借助课堂派平台,每次课前发布预习作业.而预习作业 ...
- 20145237 《Java程序设计》第10周学习总结
20145237 <Java程序设计>第10周学习总结 教材学习内容总结 Java的网络编程 •网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. •java.n ...
- git基本语法
基本用法(上) 一.实验说明 本节实验为 Git 入门第一个实验,可以帮助大家熟悉如何创建和使用 git 仓库. 二.git的初始化 在使用git进行代码管理之前,我们首先 ...
- Java基础类库简介
Java基础类库简介 一.常用的基础类库:11个jar(Java Archive,Java归档)包 作为java语言使用者,我们可以感受到java语言带来的优势(平台无关.面向对象.多线程.高效易扩展 ...
- js常用API方法
String对象常用的API:API指应用程序编程接口,实际上就是一些提前预设好的方法. charAt() 方法可返回指定位置的字符. stringObject.charAt(index) index ...