We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
can u please check what is wrong with my code. it is not working
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;classTrie{privateclasstrinode{Map<Character,trinode>children;booleanendofwrd;privatetrinode(){children=newHashMap<>();endofwrd=false;}}privatefinaltrinoderoot;publicTrie(){root=newtrinode();}publicvoidinsert(Stringword){trinodecurrent=root;for(inti=0;i<word.length();i++){charch=word.charAt(i);trinodenode=current.children.get(ch);if(node==null){node=newtrinode();current.children.put(ch,node);}current=node;}current.endofwrd=true;/* Traverse down tree to end of our prefix */}publicintfind(Stringprefix){trinodecurr=root;intc=0;for(inti=0;i<prefix.length();i++){Characterch=prefix.charAt(i);if(!curr.children.containsKey(ch)){return0;}else{curr=curr.children.get(ch);}}c=curr.children.size();if(curr.endofwrd)c=c+1;returnc;}}publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannersc=newScanner(System.in);Triets=newTrie();intq=sc.nextInt();inti,j;for(i=0;i<q;i++){Strings=sc.next();Stringcontact=sc.next();if(s.equals("add")){ts.insert(contact);}elseif(s.equals("find")){System.out.println(ts.find(contact));}}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Contacts
You are viewing a single comment's thread. Return to all comments →
can u please check what is wrong with my code. it is not working