Skip to content

Commit adb5683

Browse files
refactor 320
1 parent 9654ae0 commit adb5683

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

src/main/java/com/fishercoder/solutions/_320.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.List;
55

66
/**
7+
* 320. Generalized Abbreviation
8+
*
79
* Write a function to generate the generalized abbreviations of a word.
810
911
Example:
@@ -13,21 +15,25 @@
1315
*/
1416
public class _320 {
1517

16-
public List<String> generateAbbreviations(String word) {
17-
List<String> result = new ArrayList<>();
18-
backtrack(word, result, 0, "", 0);
19-
return result;
20-
}
18+
public static class Solution1 {
19+
public List<String> generateAbbreviations(String word) {
20+
List<String> result = new ArrayList<>();
21+
backtrack(word, result, 0, "", 0);
22+
return result;
23+
}
2124

22-
private void backtrack(String word, List<String> result, int position, String current, int count) {
23-
if (position == word.length()) {
24-
if (count > 0) {
25-
current += count;
25+
private void backtrack(String word, List<String> result, int position, String current,
26+
int count) {
27+
if (position == word.length()) {
28+
if (count > 0) {
29+
current += count;
30+
}
31+
result.add(current);
32+
} else {
33+
backtrack(word, result, position + 1, current, count + 1);
34+
backtrack(word, result, position + 1,
35+
current + (count > 0 ? count : "") + word.charAt(position), 0);
2636
}
27-
result.add(current);
28-
} else {
29-
backtrack(word, result, position + 1, current, count + 1);
30-
backtrack(word, result, position + 1, current + (count > 0 ? count : "") + word.charAt(position), 0);
3137
}
3238
}
3339

src/test/java/com/fishercoder/_320Test.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,30 @@
1111

1212
import static org.junit.Assert.assertTrue;
1313

14-
/**
15-
* Created by fishercoder on 2/10/17.
16-
*/
1714
public class _320Test {
18-
private static _320 test;
19-
private static List<String> expected;
20-
private static List<String> actual;
21-
private static String word;
15+
private static _320.Solution1 solution1;
16+
private static List<String> expected;
17+
private static List<String> actual;
18+
private static String word;
2219

23-
@BeforeClass
24-
public static void setup() {
25-
test = new _320();
26-
}
20+
@BeforeClass
21+
public static void setup() {
22+
solution1 = new _320.Solution1();
23+
}
2724

28-
@Before
29-
public void setupForEachTest() {
30-
expected = new ArrayList<>();
31-
actual = new ArrayList<>();
32-
}
25+
@Before
26+
public void setupForEachTest() {
27+
expected = new ArrayList<>();
28+
actual = new ArrayList<>();
29+
}
3330

34-
@Test
35-
public void test1() {
36-
word = "word";
37-
expected = Arrays.asList("word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4");
38-
actual = test.generateAbbreviations(word);
39-
assertTrue(expected.containsAll(actual) && actual.containsAll(expected));
40-
}
31+
@Test
32+
public void test1() {
33+
word = "word";
34+
expected =
35+
Arrays.asList("word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1",
36+
"w1r1", "1o2", "2r1", "3d", "w3", "4");
37+
actual = solution1.generateAbbreviations(word);
38+
assertTrue(expected.containsAll(actual) && actual.containsAll(expected));
39+
}
4140
}

0 commit comments

Comments
 (0)