Can u tell me why Trie is not giving the correct answer for all the test cases or why my code is not giving the correct answer for all the test cases
"
import java.util.*;
public class STringWindow {
static int count = Integer.MAX_VALUE;
public class Node {
char data;
HashMap<Character, Node> children;
boolean isTerminal;
public Node(char data, boolean isTerminal) {
this.data = data;
this.isTerminal = isTerminal;
this.children = new HashMap<>();
}
}
private int numOfWords;
private Node root;
public STringWindow() {
// TODO Auto-generated constructor stub
this.root = new Node('\0', false);
this.numOfWords = 0;
}
public void addWord(String word) {
addWord(this.root, word);
}
private void addWord(Node parent, String word) {
// TODO Auto-generated method stub
if (word.length() == 0) {
if (!parent.isTerminal) {
parent.isTerminal = true;
this.numOfWords++;
}
return;
}
char cc = word.charAt(0);
String ros = word.substring(1);
Node child = parent.children.get(cc);
if (child == null) {
child = new Node(cc, false);
parent.children.put(cc, child);
}
addWord(child, ros);
}
public void display() {
display(this.root, "");
}
private void display(Node parent, String ans) {
if (parent.isTerminal) {
System.out.println(ans.substring(1) + parent.data);
}
Set<Map.Entry<Character, Node>> entries = root.children.entrySet();
for (Map.Entry<Character, Node> entry : entries) {
display(entry.getValue(), ans + parent.data);
}
}
public void stringwindow(String find) {
String[] fans = { "" };
stringwindow(this.root, find, "", fans);
if (count == Integer.MAX_VALUE) {
System.out.println("No string");
} else {
System.out.println(fans[0]);
}
}
private void stringwindow(Node parent, String find, String ans, String[] fans) {
// TODO Auto-generated method stub
if (parent.isTerminal && find.length() != 0) {
return;
}
if (find.length() == 0) {
if (ans.length() < count) {
fans[0] = ans;
count = ans.length();
}
return;
}
Set<Map.Entry<Character, Node>> entries = parent.children.entrySet();
for (Map.Entry<Character, Node> entry : entries) {
char c = entry.getKey();
if (c == find.charAt(0)) {
String ros = find.substring(1);
stringwindow(entry.getValue(), ros, ans + c, fans);
} else {
stringwindow(entry.getValue(), find, ans + c, fans);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
STringWindow ts = new STringWindow();
String str = scn.nextLine();
String sub = scn.nextLine();
String[] currencies = str.split(" ");
for (int i = 0; i < currencies.length; i++) {
ts.addWord(currencies[i]);
}
ts.stringwindow(sub);
}
}
"