forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_320.java
30 lines (25 loc) · 929 Bytes
/
_320.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _320 {
public static class Solution1 {
public List<String> generateAbbreviations(String word) {
List<String> result = new ArrayList<>();
backtrack(word, result, 0, "", 0);
return result;
}
private void backtrack(String word, List<String> result, int position, String current,
int count) {
if (position == word.length()) {
if (count > 0) {
current += count;
}
result.add(current);
} else {
backtrack(word, result, position + 1, current, count + 1);
backtrack(word, result, position + 1,
current + (count > 0 ? count : "") + word.charAt(position), 0);
}
}
}
}