-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_320.java
33 lines (29 loc) · 1 KB
/
_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
31
32
33
package com.fishercoder.solutions.firstthousand;
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);
}
}
}
}