I am confident that my code is correct
After compiling my answer is coming correct...but after submitting showing that failed test case...why?
please share the question link as well as your solution
how to share the question link?
import java.util.*;
public class Main {
private class Node{
char data;
HashMap<Character,Node> children;
boolean isTerminal;
Node(char data, boolean isTerminal){
this.data=data;
this.children=new HashMap<>();
this.isTerminal=isTerminal;
}
}
private int numWords;
private Node root;
Main(){
this.root=new Node('\0',false);
this.numWords=0;
}
public int numWords(){
return this.numWords;
}
public void addWord(String word){
this.addWord(this.root,word);
}
private void addWord(Node parent,String word)
{
if(word.length()==0)
{
if(parent.isTerminal){
}else{
parent.isTerminal=true;
this.numWords++;
}
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);
}
this.addWord(child,ros);
}
public boolean search(String word)
{
return this.search(this.root,word,"");
}
private boolean search(Node parent, String word,String osf){
if(word.length()==0)
{
int l1=osf.length();
if(l1>1)
{
this.display(parent,osf.substring(0,l1-1));
}else{
this.display(parent,"");
}
return true;
}
char cc=word.charAt(0);
osf=osf+cc;
String ros=word.substring(1);
Node child=parent.children.get(cc);
if(child==null)
{
return false;
}
return this.search(child,ros,osf);
}
public static ArrayList<String> fans=new ArrayList<>();
private void display(Node node,String osf)
{
if(node.isTerminal){
String todisplay=osf+node.data;
fans.add(todisplay);
}
for(Map.Entry<Character,Node> entries:node.children.entrySet())
{
this.display(entries.getValue(),osf+node.data);
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Main trie=new Main();
for(int i=0;i<n;i++)
{
trie.addWord(sc.next());
}
int m=sc.nextInt();
for(int i=0;i<m;i++)
{
String s=sc.next();
if(!trie.search(s))
{
fans.add("No suggestions");
}
}
for(String str:fans)
{
System.out.println(str);
}
}
}
Okay I will see the code and question
You are confident that your code is correct, right?
Sometimes there is an issue with the test cases also. But I will check and see
yes sir, code is right…I compiled it with the given example , and the correct output is being shown
Also tried running in eclipse…its running properly
It is not an error but try to create a new dictionary class and move the functionality into that. Leave only the main function in the Main class. Maybe since you have done everything inside main it may be clashing with the code they have written on the server side.