comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
中等 |
|
单词的 广义缩写词 可以通过下述步骤构造:先取任意数量的 不重叠、不相邻 的子字符串,再用它们各自的长度进行替换。
- 例如,
"abcde"
可以缩写为:<ul> <li><code>"a3e"</code>(<code>"bcd"</code> 变为 <code>"3"</code> )</li> <li><code>"1bcd1"</code>(<code>"a"</code> 和 <code>"e"</code> 都变为 <code>"1"</code>)<meta charset="UTF-8" /></li> <li><code>"5"</code> (<code>"abcde"</code> 变为 <code>"5"</code>)</li> <li><code>"abcde"</code> (没有子字符串被代替)</li> </ul> </li> <li>然而,这些缩写是 <strong>无效的</strong> : <ul> <li><code>"23"</code>(<code>"ab"</code> 变为 <code>"2"</code> ,<code>"cde"</code> 变为 <code>"3"</code> )是无效的,因为被选择的字符串是相邻的</li> <li><meta charset="UTF-8" /><code>"22de"</code> (<code>"ab"</code> 变为 <code>"2"</code> , <code>"bc"</code> 变为 <code>"2"</code>) 是无效的,因为被选择的字符串是重叠的</li> </ul> </li>
给你一个字符串 word
,返回 一个由 word
的所有可能 广义缩写词 组成的列表 。按 任意顺序 返回答案。
示例 1:
输入:word = "word" 输出:["4","3d","2r1","2rd","1o2","1o1d","1or1","1ord","w3","w2d","w1r1","w1rd","wo2","wo1d","wor1","word"]
示例 2:
输入:word = "a" 输出:["1","a"]
提示:
1 <= word.length <= 15
word
仅由小写英文字母组成
我们设计一个函数
函数
如果
否则,我们可以选择保留
我们也可以选择删除
最后,我们在主函数中调用
时间复杂度
class Solution:
def generateAbbreviations(self, word: str) -> List[str]:
def dfs(i: int) -> List[str]:
if i >= n:
return [""]
ans = [word[i] + s for s in dfs(i + 1)]
for j in range(i + 1, n + 1):
for s in dfs(j + 1):
ans.append(str(j - i) + (word[j] if j < n else "") + s)
return ans
n = len(word)
return dfs(0)
class Solution {
private String word;
private int n;
public List<String> generateAbbreviations(String word) {
this.word = word;
n = word.length();
return dfs(0);
}
private List<String> dfs(int i) {
if (i >= n) {
return List.of("");
}
List<String> ans = new ArrayList<>();
for (String s : dfs(i + 1)) {
ans.add(String.valueOf(word.charAt(i)) + s);
}
for (int j = i + 1; j <= n; ++j) {
for (String s : dfs(j + 1)) {
ans.add((j - i) + "" + (j < n ? String.valueOf(word.charAt(j)) : "") + s);
}
}
return ans;
}
}
class Solution {
public:
vector<string> generateAbbreviations(string word) {
int n = word.size();
function<vector<string>(int)> dfs = [&](int i) -> vector<string> {
if (i >= n) {
return {""};
}
vector<string> ans;
for (auto& s : dfs(i + 1)) {
string p(1, word[i]);
ans.emplace_back(p + s);
}
for (int j = i + 1; j <= n; ++j) {
for (auto& s : dfs(j + 1)) {
string p = j < n ? string(1, word[j]) : "";
ans.emplace_back(to_string(j - i) + p + s);
}
}
return ans;
};
return dfs(0);
}
};
func generateAbbreviations(word string) []string {
n := len(word)
var dfs func(int) []string
dfs = func(i int) []string {
if i >= n {
return []string{""}
}
ans := []string{}
for _, s := range dfs(i + 1) {
ans = append(ans, word[i:i+1]+s)
}
for j := i + 1; j <= n; j++ {
for _, s := range dfs(j + 1) {
p := ""
if j < n {
p = word[j : j+1]
}
ans = append(ans, strconv.Itoa(j-i)+p+s)
}
}
return ans
}
return dfs(0)
}
function generateAbbreviations(word: string): string[] {
const n = word.length;
const dfs = (i: number): string[] => {
if (i >= n) {
return [''];
}
const ans: string[] = [];
for (const s of dfs(i + 1)) {
ans.push(word[i] + s);
}
for (let j = i + 1; j <= n; ++j) {
for (const s of dfs(j + 1)) {
ans.push((j - i).toString() + (j < n ? word[j] : '') + s);
}
}
return ans;
};
return dfs(0);
}
由于字符串
时间复杂度
class Solution:
def generateAbbreviations(self, word: str) -> List[str]:
n = len(word)
ans = []
for i in range(1 << n):
cnt = 0
s = []
for j in range(n):
if i >> j & 1:
cnt += 1
else:
if cnt:
s.append(str(cnt))
cnt = 0
s.append(word[j])
if cnt:
s.append(str(cnt))
ans.append("".join(s))
return ans
class Solution {
public List<String> generateAbbreviations(String word) {
int n = word.length();
List<String> ans = new ArrayList<>();
for (int i = 0; i < 1 << n; ++i) {
StringBuilder s = new StringBuilder();
int cnt = 0;
for (int j = 0; j < n; ++j) {
if ((i >> j & 1) == 1) {
++cnt;
} else {
if (cnt > 0) {
s.append(cnt);
cnt = 0;
}
s.append(word.charAt(j));
}
}
if (cnt > 0) {
s.append(cnt);
}
ans.add(s.toString());
}
return ans;
}
}
class Solution {
public:
vector<string> generateAbbreviations(string word) {
int n = word.size();
vector<string> ans;
for (int i = 0; i < 1 << n; ++i) {
string s;
int cnt = 0;
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
++cnt;
} else {
if (cnt) {
s += to_string(cnt);
cnt = 0;
}
s.push_back(word[j]);
}
}
if (cnt) {
s += to_string(cnt);
}
ans.push_back(s);
}
return ans;
}
};
func generateAbbreviations(word string) (ans []string) {
n := len(word)
for i := 0; i < 1<<n; i++ {
s := &strings.Builder{}
cnt := 0
for j := 0; j < n; j++ {
if i>>j&1 == 1 {
cnt++
} else {
if cnt > 0 {
s.WriteString(strconv.Itoa(cnt))
cnt = 0
}
s.WriteByte(word[j])
}
}
if cnt > 0 {
s.WriteString(strconv.Itoa(cnt))
}
ans = append(ans, s.String())
}
return
}