[LeetCode] 527. 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] 527. Word Abbreviation 单词缩写的更多相关文章
- [LeetCode] Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [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 ...
- [LeetCode] 79. 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] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
随机推荐
- Leetcode 542:01 矩阵 01
Leetcode 542:01 矩阵 01 Matrix### 题目: 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . Given a matr ...
- Python自定义注解
Python3.0之后加入新特性Decorators,以@为标记修饰function和class.有点类似c++的宏和java的注解.Decorators用以修饰约束function和class,分为 ...
- IDEA不能读取配置文件,springboot配置文件无效、IDEA resources文件夹指定
- Window权限维持(六):BITS Jobs
Windows操作系统包含各种实用程序,系统管理员可以使用它们来执行各种任务.这些实用程序之一是后台智能传输服务(BITS),它可以促进文件到Web服务器(HTTP)和共享文件夹(SMB)的传输能力. ...
- Java面向对象——类的成员
Java面向对象——类的成员 摘要:本文主要介绍了类的常见成员. 属性 属性称为成员变量,一般来讲不用赋值,因为有默认值,另外显式赋值没有意义会导致所有由此类创建对象都是此值. 默认值 Boolean ...
- MySQL基础(三)(操作数据表中的记录)
1.插入记录INSERT 命令:,expr:表达式 注意:如果给主键(自动编号的字段)赋值的话,可以赋值‘NULL’或‘DEFAULT’,主键的值仍会遵守默认的规则:如果省略列名的话,所有的字段必须一 ...
- 查看 JVM 默认参数
-XX:+PrintFlagsFinal 可以获取所有可设置参数及值 获取 JVM 默认 Xss 大小 java -XX:+PrintFlagsFinal -version | grep Thread ...
- nginx配置多个静态资源
#user nobody; worker_processes ; worker_cpu_affinity ; #error_log logs/error.log; #error_log logs/er ...
- Centos 7 下yum搭建lnmp环境(yum安装方式)
我们都知道linux下安装软件主要有三种方式: 1.源码编译安装,即下载软件源代码,利用gcc g++ make 等编译工具进行编译安装: 此方式的优点:可以指定软件版本,可选择性好:编译时可以手动指 ...
- 习题6-2 使用函数求特殊a串数列和
#include <stdio.h> int fn(int a, int n); int SumA(int a, int n); int main() { int a, n; scanf_ ...