320: 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"]
public List<String> generateAbbreviations(String word) {
List<String> res = new ArrayList<>();
DFS(res, new StringBuilder(), word.toCharArray(), 0, 0);
return res;
}
public void DFS(List<String> res, StringBuilder sb, char[] c, int i, int num) {
int len = sb.length();
if(i == c.length) {
if(num != 0) sb.append(num);
res.add(sb.toString());
} else {
DFS(res, sb, c, i + 1, num + 1); // abbr c[i]
if(num != 0) sb.append(num); // not abbr c[i]
DFS(res, sb.append(c[i]), c, i + 1, 0);
}
sb.setLength(len);
}
https://leetcode.com/discuss/76783/java-14ms-beats-100%25
Group: remove parentheses:
https://leetcode.com/discuss/72208/easiest-9ms-java-solution
add operators:
https://leetcode.com/discuss/75308/java-simple-and-fast-solution-beats-96-56%25