forked from ashoklathwal/Code-for-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie-contacts.java
41 lines (39 loc) · 1.18 KB
/
Trie-contacts.java
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Trie
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
HashMap<String,Integer> hm=new HashMap<>();
for(int i=0;i<n;i++)
{
String str1=sc.next();
if(str1.equals("add"))
{
String str2=sc.next();
int length=str2.length();
for(int j=0;j<length;j++)
{
String sbstr=str2.substring(0,j+1);
if(hm.get(sbstr)==null)
hm.put(sbstr,1);
else
hm.put(sbstr,hm.get(sbstr)+1);
}
}
else
{
String str2=sc.next();
if(hm.containsKey(str2))
System.out.println(hm.get(str2));
else
System.out.println("0");
}
}
}
}