-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEncodingCombinations.java
More file actions
executable file
·52 lines (45 loc) · 1002 Bytes
/
Copy pathEncodingCombinations.java
File metadata and controls
executable file
·52 lines (45 loc) · 1002 Bytes
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
package com.vee.algorithms.dynprog;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
/**
* 123 -> abc, lc, aw
*
*/
public class EncodingCombinations {
void show(int n) {
String nstr = n + "";
List<String> combinations = new ArrayList<String>();
int i = 0;
int num = nstr.charAt(i) - 48;
combinations.add(toChar(num));
for (i = 1; i < nstr.length(); i++) {
num = nstr.charAt(i) - 48;
List<String> temp = new ArrayList<String>();
for (String s : combinations) {
int code = toInt(s.charAt(s.length()-1));
int newcode = code * 10 + num;
if (newcode < 27) {
temp.add(s.substring(0, s.length()-1) + toChar(newcode));
}
temp.add(s + toChar(num));
}
n = n / 10;
combinations = temp;
}
for (String s : combinations) {
System.out.println(s);
}
}
int toInt(char ch) {
return ch - 96;
}
String toChar(int i) {
return (char) (i + 96) + "";
}
@Test
public void testShow() {
show(123);
show(616);
}
}