Write a function to generate the generalized abbreviations of a word.

Note: The order of the output does not matter.

Example:

Input: "word"
Output:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

这道题让我们对一个单词进行部分简写,简写的规则是若干个字母可以用数字来表示,但是不能有两个相邻的数字,具体可以参考题目中给的例子,根据我以往的经验,这种列举所有情况的必定是要用DFS来写的,但是我一时半会又没想到该咋递归,后来我数了一下题目中给的例子的所有情况的个数,是16个,而word有4个字母,刚好是2的4次方,这是巧合吗,当然不是,后来我又发现如果把0到15的二进制写出来,每一个可以对应一种情况,如下所示:

0000 word
0001 wor1
0010 wo1d
0011 wo2
0100 w1rd
0101 w1r1
0110 w2d
0111 w3
1000 1ord
1001 1or1
1010 1o1d
1011 1o2
1100 2rd
1101 2r1
1110 3d
1111 4

那么我们就可以观察出规律,凡是0的地方都是原来的字母,单独的1还是1,如果是若干个1连在一起的话,就要求出1的个数,用这个数字来替换对应的字母,既然规律找出来了,那么代码就很好写了,如下所示:

解法一:

class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
for (int i = ; i < pow(, word.size()); ++i) {
string out = "";
int cnt = , t = i;
for (int j = ; j < word.size(); ++j) {
if (t & == ) {
++cnt;
if (j == word.size() - ) out += to_string(cnt);
} else {
if (cnt != ) {
out += to_string(cnt);
cnt = ;
}
out += word[j];
}
t >>= ;
}
res.push_back(out);
}
return res;
}
};

上述方法返回结果的顺序为:

["word","1ord","w1rd","2rd","wo1d","1o1d","w2d","3d","wor1","1or1","w1r1","2r1","wo2","1o2","w3","4"]

我们可以对上面代码稍稍改写一下,变的稍微简洁一点:

解法二:

class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
for (int i = ; i < pow(, word.size()); ++i) {
string out = "";
int cnt = ;
for (int j = ; j < word.size(); ++j) {
if ((i >> j) & ) ++cnt;
else {
if (cnt != ) {
out += to_string(cnt);
cnt = ;
}
out += word[j];
}
}
if (cnt > ) out += to_string(cnt);
res.push_back(out);
}
return res;
}
};

那么迭代的写法看完了,来考虑一些递归的写法吧,上网搜了一下,发现下面三种写法比较容易理解,

解法三:

class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res{word};
helper(word, , res);
return res;
}
void helper(string word, int pos, vector<string> &res) {
for (int i = pos; i < word.size(); ++i) {
for (int j = ; i + j <= word.size(); ++j) {
string t = word.substr(, i);
t += to_string(j) + word.substr(i + j);
res.push_back(t);
helper(t, i + + to_string(j).size(), res);
}
}
}
};

上述方法返回结果的顺序为:

["word","1ord","1o1d","1o2","1or1","2rd","2r1","3d","4","w1rd","w1r1","w2d","w3","wo1d","wo2","wor1"]

解法四:

class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
helper(word, , , "", res);
return res;
}
void helper(string word, int pos, int cnt, string out, vector<string> &res) {
if (pos == word.size()) {
if (cnt > ) out += to_string(cnt);
res.push_back(out);
} else {
helper(word, pos + , cnt + , out, res);
helper(word, pos + , , out + (cnt > ? to_string(cnt) : "") + word[pos], res);
}
}
};

上述方法返回结果的顺序为:

["4","3d","2r1","2rd","1o2","1o1d","1or1","1ord","w3","w2d","w1r1","w1rd","wo2","wo1d","wor1","word"]

解法五:

class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
res.push_back(word.size() == ? "" : to_string(word.size()));
for (int i = ; i < word.size(); ++i) {
for (auto a : generateAbbreviations(word.substr(i + ))) {
string left = i > ? to_string(i) : "";
res.push_back(left + word.substr(i, ) + a);
}
}
return res;
}
};

上述方法返回结果的顺序为:

["4","w3","wo2","wor1","word","wo1d","w1r1","w1rd","w2d","1o2","1or1","1ord","1o1d","2r1","2rd","3d"]

参考资料:

https://leetcode.com/problems/generalized-abbreviation/

https://leetcode.com/discuss/75754/java-backtracking-solution

https://leetcode.com/discuss/77280/c-straightforward-recursive-solution

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

[LeetCode] Generalized Abbreviation 通用简写的更多相关文章

  1. LeetCode Generalized Abbreviation

    原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...

  2. [LeetCode] Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  3. [Swift]LeetCode320. 通用简写 $ Generalized Abbreviation

    Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...

  4. LeetCode 320. Generalized Abbreviation

    原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...

  5. 【LeetCode】320. Generalized Abbreviation 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  6. LeetCode Word Abbreviation

    原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...

  7. 320. Generalized Abbreviation

    首先想到的是DFS,对于每个单词的字母都遍历,比如 spy: 1py,s1y,sp1 然后每个遍历完的单词再DFS..左右有数字就合并比如 1py: 11y=>2py, 1p1 这样.. 但是单 ...

  8. [Locked] Generalized Abbreviation

    Write a function to generate the generalized abbreviations of a word. Example:Given word = "wor ...

  9. LeetCode 527---Word Abbreviation

    527. Word Abbreviation Given an array of n distinct non-empty strings, you need to generate minimal ...

随机推荐

  1. Vertica DBD 分析优化设计

    DBD = Database Designer,是Vertica数据库优化中最主要的原生工具. 首先运行admintools工具,按下面步骤依次执行: 1.选择"6 Configuratio ...

  2. C#基础回顾(三)—索引器、委托、反射

    一.前言                                                                                       ------人生路 ...

  3. 分页插件思想:pc加载更多功能和移动端下拉刷新加载数据

    感觉一个人玩lol也没意思了,玩会手机,看到这个下拉刷新功能就写了这个demo! 这个demo写的比较随意,咱不能当做插件使用,基本思想是没问题的,要用就自己封装吧! 直接上代码分析下吧! 布局: & ...

  4. 你真的会玩SQL吗?简单的数据修改

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  5. 没有神话,聊聊decimal的“障眼法”

    0x00 前言 在上一篇文章<妥协与取舍,解构C#中的小数运算>的留言区域有很多朋友都不约而同的说道了C#中的decimal类型.事实上之前的那篇文章的立意主要在于聊聊使用二进制的计算机是 ...

  6. Java中的泛型 (上) - 基本概念和原理

    本节我们主要来介绍泛型的基本概念和原理 后续章节我们会介绍各种容器类,容器类可以说是日常程序开发中天天用到的,没有容器类,难以想象能开发什么真正有用的程序.而容器类是基于泛型的,不理解泛型,我们就难以 ...

  7. Lua 安全调用 metatable 的简单应用

    事情的经过 我们的项目中存在好几个战斗界面,不过界面中的内容略有不同.跟同事出去吃饭的时候,他问我.我们现在的战斗界面.有很多是重复的,但是也有偶尔几个地方不太一样.我在战斗过程中驱动这些界面的时候. ...

  8. 03 通过Button打开另一个的frm

    private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult re = MessageBox ...

  9. ASP.NET Core File Providers

    原文地址:FileProvider By Steve Smith ASP.NET Core通过对File Providers的使用实现了对文件系统访问的抽象. 查看或下载示例代码 File Provi ...

  10. Aspose.Cells导出Excel(1)

    利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...