LeetCode Generalized Abbreviation

时间:2023-03-08 16:17:54

原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/

题目:

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

Example:

Given word = "word", return the following list (order does not matter):

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

题解:

For DFS, the state needs word, current index, current string, res and count of abbreviated char.

对于当前char有两个选择: 第一缩写当前char, count+1, index+1. 第二不缩写当前char, 先append 非零的count再append当前char, count回零, index+1.

Backtracking时因为直接用string, 都是copy所以不担心.

Time Complexity: exponential. 在每一步递归时,有两个branches. 递归树全部展开会有2^n个叶子. 每一个叶子都相当于用了O(n)时长, 把StringBuilder变成String就用时O(n). n = word.length().

Space: O(n). n层stack, char array size也是n. regardless res.

AC Java:

 class Solution {
public List<String> generateAbbreviations(String word) {
List<String> res = new ArrayList<String>();
if(word == null || word.length() == 0){
res.add("");
return res;
} dfs(word, 0, 0, "", res);
return res;
} private void dfs(String word, int i, int count, String cur, List<String> res){
if(i == word.length()){
if(count != 0){
cur += count;
} res.add(cur);
return;
} dfs(word, i+1, count+1, cur, res); if(count != 0){
cur += count;
} cur += word.charAt(i);
dfs(word, i+1, 0, cur, res);
}
}

Bit Manipulation 方法是用binary表示word的每一位. 0代表不缩写当前char, 1代表缩写当前char.

word 若是用 0011表示就是不缩写wo, 缩写rd, 最后是wo2.

对于word的所有binary表示, 也就是0000到1111走一遍,每个binary变成string.

Time Complexity: O(n*2^n). n = word.length(), 一共有O(2^n)个binary表示. 每个binary变成string用时O(n).

Space: O(n), regardless res.

AC Java:

 public class Solution {
public List<String> generateAbbreviations(String word) {
List<String> res = new ArrayList<String>();
if(word == null){
return res;
}
int n = 1<<word.length();
char [] s = word.toCharArray();
for(int i = 0; i<n; i++){
res.add(abbr(i, s));
}
return res;
} private String abbr(int num, char [] s){
StringBuilder sb = new StringBuilder();
int count = 0;
for(int i = 0; i<s.length; i++, num>>=1){
// 0 表示不缩写当前char, 1 表示缩写当前char
if((num & 1) == 0){
// 先加上之前的非零count, 再把count清零
if(count != 0){
sb.append(count);
count = 0;
}
sb.append(s[i]);
}else{
count++;
}
}
//最后的非零count不要忘记加进sb中
if(count != 0){
sb.append(count);
}
return sb.toString();
}
}