my solution passing all test from my head
can you advice what's wrong:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static java.util.stream.Collectors.toList;
/**
* solution for
http://acm.timus.ru/problem.aspx?space=1&num=1002 */
public class Step {
private static Map<Character, List<Character>> mapping;
public static void main(String[] args) throws IOException {
new Step().solveMe(new BufferedReader(new InputStreamReader(System.in)));
}
public static Map<Character, List<Character>> buildMapping() {
Map<Character, List<Character>> map = new HashMap<>();
map.put('1', Arrays.asList('i', 'j'));
map.put('2', Arrays.asList('a', 'b', 'c'));
map.put('3', Arrays.asList('d', 'e', 'f'));
map.put('4', Arrays.asList('g', 'h'));
map.put('5', Arrays.asList('k', 'l'));
map.put('6', Arrays.asList('m', 'n'));
map.put('7', Arrays.asList('p', 'r', 's'));
map.put('8', Arrays.asList('t', 'u', 'v'));
map.put('9', Arrays.asList('w', 'x', 'y'));
map.put('0', Arrays.asList('o', 'q', 'z'));
return map;
}
public static Trie buildTrie(List<String> dictionary) {
return dictionary.stream().reduce(
Trie.EMPTY_TRIE, (node, word) -> node.buildTree(node, word),
(node, node2) -> node.append(node));
}
private void solveMe(BufferedReader in) throws IOException {
String n;
mapping = buildMapping();
Map<String, Trie> input = new HashMap<>();
while (!(n = in.readLine()).equals("-1")) {
int s = Integer.parseInt(in.readLine());
List<String> dict = in.lines().limit(s).collect(toList());
input.compute(n, (key, val) -> buildTrie(dict));
}
input.forEach((s, trie) -> System.out.println(solution(s, trie)));
}
public String solution(String number, Trie root) {
List<List<String>> result = root.filter(number);
if (result.isEmpty()) {
return "No solution.";
}
List<String> bestShot = result.stream().reduce((List<String> minList, List<String> other) -> {
if (minList.size() >= other.size()) {
return other;
}
return minList;
}).get();
return String.join(" ", bestShot);
}
public static class Trie {
public static final Trie EMPTY_TRIE = new Trie((char) 0);
private final Character ch;
private final Map<Character, Trie> nexts;
private final boolean isWord;
public Trie(Character ch) {
this(ch, new HashMap<>(), false);
}
private Trie(Character ch, boolean isWord) {
this(ch, new HashMap<>(), isWord);
}
private Trie(Character ch, Map<Character, Trie> nexts, boolean isWord) {
this.ch = ch;
this.nexts = nexts;
this.isWord = isWord;
}
public List<List<String>> filter(String number) {
List<List<String>> lists = new ArrayList<>();
List<String> e = new ArrayList<>();
filterHelper(lists, e, number, "", this);
return lists;
}
private void filterHelper(List<List<String>> acc, List<String> result, String number, String word, Trie node) {
if (node.isWord) {
if (number.length() == 0) {
result.add(word);
acc.add(result);
return;
}
List<String> res = new ArrayList<>(result);
res.add(word);
filterHelper(acc, res, number, "", this);
}
List<Character> maps = mapping.get(number.charAt(0));
maps.forEach(e -> {
Trie next = node.nexts.get(e);
if (next != null) {
filterHelper(acc, new ArrayList<>(result), number.substring(1), word + next.ch, next);
}
});
}
public Trie buildTree(Trie root, String word) {
char c = word.charAt(0);
if (word.length() == 1) {
Trie newTrie = new Trie(c, true);
return root.append(newTrie);
} else {
Trie newTrie = new Trie(c, false);
return root.append(buildTree(newTrie, word.substring(1)));
}
}
public Trie append(Trie other) {
Trie trieCopy = new Trie(this.ch, this.nexts, this.isWord);
Trie newVal = other;
if (trieCopy.nexts.containsKey(newVal.ch)) {
Trie next = trieCopy.nexts.get(newVal.ch);
Trie initial = new Trie(next.ch, next.nexts, next.isWord);
newVal = newVal.nexts.values().stream().reduce(initial, Trie::append);
}
trieCopy.nexts.put(newVal.ch, newVal);
return trieCopy;
}
@Override
public String toString() {
StringBuilder bld = new StringBuilder();
bld.append(ch).append(" = [");
nexts.forEach((c, n) -> bld.append(n).append(" "));
return bld.append("]").append(isWord ? " = true" : "").toString();
}
}
}