[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 ...
随机推荐
- Sweet Butter 香甜的黄油
Sweet Butter 香甜的黄油 题目大意:m个点,n头奶牛,p条边,每一头奶牛在一个点上,一个点可以有多只奶牛,求这样一个点,使得所有奶牛到这个点的距离之和最小. 注释:n<=500 , ...
- JS获得一个对象的所有属性和方法
function displayProp(obj){ var names=""; for(var name in obj){ names+=name+": "+ ...
- win10下NeuralStyle的tensorflow版实验
---恢复内容开始--- 首先配置win10下的tensorflow-gpu的运行环境,然后在github上将NeuralStyle拷贝下来,最后根据文档说明参数,运行文件,即可得到自己喜欢的styl ...
- UGUI中显示粒子特效
今天在UGUI上显示粒子特效的时候遇到的一些问题,Mark一下.原理:修改特效中每一个ParticleSystem的Layer为UI,并且把ParticleSystemRenderer.sorting ...
- fs 创建文件夹
var http = require("http"); var fs = require("fs"); var server = http.createServ ...
- 关于GPUImage的导入
对于GPUImage的使用方面,GitHub上已经非常详细了,就不一一赘述了,但是对于项目的导入来说,最好的方式是 1.下载GPUImage并解压 2.打开压缩包后如图 3.打开终端,cd到此目录 4 ...
- Vue.js和jQuery混合使用的一点注意事项
首先,Vue 的官方是不建议直接操作 DOM 的,其优势在于视图和数据的双向绑定,而且所有DOM操作都可以用Vue实现,反而使用jQuery来操作DOM的话,会造成不必要的麻烦,DOM未渲染完成之前事 ...
- 自动化服务部署(一):Linux下安装JDK
自动化测试的主要目的是为了执行回归测试.当然,为了模拟真实的用户操作,一般都是在UAT或者生产环境进行回归测试. 为了尽量避免内网和外网解析对测试结果的影响,将自动化测试服务部署在外网的服务器是比较好 ...
- [UWP]针对UWP程序多语言支持的总结,含RTL
UWP 对 Globalization and localization 的支持非常好,可以非常容易地实现应用程序本地化. 所谓本地化,表现最为直观的就是UI上文字和布局方式了,针对文字,提供不同的语 ...
- 一种dubbo逻辑路由方案
背景介绍 现在很多的公司都在用dubbo.springcloud做为服务化/微服务的开发框架,服务化之后应用越来越多,链路越来越长,服务环境的治理变的很困难.比如:研发团队的人很多的,同时有几个分支在 ...