-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathGeneralizedAbbreviation320.java
74 lines (64 loc) · 2.3 KB
/
GeneralizedAbbreviation320.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* 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"]
*/
public class GeneralizedAbbreviation320 {
public List<String> generateAbbreviations(String word) {
List<String> res = new ArrayList<>();
int N = word.length();
backtrace(word.toCharArray(), res, true, 0, new StringBuilder());
return res;
}
private void backtrace(char[] word, List<String> res, boolean prevIsWord, int i, StringBuilder sb) {
if (word.length == i) {
res.add(sb.toString());
return;
}
sb.append(word[i]);
backtrace(word, res, true, i+1, sb);
sb.deleteCharAt(sb.length() - 1);
if (prevIsWord) {
for (int j=i; j<word.length; j++) {
int num = j-i+1;
int len = sb.length();
sb.append(num);
backtrace(word, res, false, j+1, sb);
sb.setLength(len);
}
}
}
/**
* https://leetcode.com/problems/generalized-abbreviation/solution/
*/
public List<String> generateAbbreviations2(String word) {
List<String> ans = new ArrayList<>();
for (int x = 0; x < (1 << word.length()); ++x) // loop through all possible x
ans.add(abbr(word, x));
return ans;
}
// build the abbreviation for word from number x
private String abbr(String word, int x) {
StringBuilder builder = new StringBuilder();
int k = 0, n = word.length(); // k is the count of consecutive ones in x
for (int i = 0; i < n; ++i, x >>= 1) {
if ((x & 1) == 0) { // bit is zero, we keep word.charAt(i)
if (k != 0) { // we have abbreviated k characters
builder.append(k);
k = 0; // reset the counter k
}
builder.append(word.charAt(i));
}
else // bit is one, increase k
++k;
}
if (k != 0) builder.append(k); //don't forget to append the last k if non zero
return builder.toString();
}
}