forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContacts
109 lines (96 loc) · 3.34 KB
/
Contacts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// https://www.hackerrank.com/challenges/contacts/problem
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
import java.util.List;
import java.util.ArrayList;
public class Solution {
public static class TrieNode {
Map<Character,TrieNode> children;
boolean endOfWord;
int count;
public TrieNode() {
children = new HashMap<Character,TrieNode>();
endOfWord = false;
count = 0;
}
}
public static TrieNode root;
// public Solution() {
// root = new TrieNode();
// }
public static void insert(String word) {
TrieNode current = root;
for(int i=0;i<word.length();i++) {
char ch = word.charAt(i);
TrieNode node;
//System.out.println(current.children);
if(current.children.size()>0)
node = current.children.get(ch);
else {
node = new TrieNode();
current.children.put(ch,node);
current.count = current.count + 1;
}
current = node;
}
current.endOfWord = true;
}
static int search(String word) {
TrieNode current = root;
for(int i=0;i<word.length();i++) {
char ch = word.charAt(i);
TrieNode node;
if(current.children.containsKey(ch)==true)
node = current.children.get(ch);
else
return 0;
current = node;
}
return current.children.size();
}
static int[] contacts(String[][] queries) {
root = new TrieNode();
List<Integer> result = new ArrayList<Integer>();
for(int i=0;i<queries.length;i++) {
for(int j=0;j<2;j++)
{
if(queries[i][0].equals("add"))
insert(queries[i][1]);
else {
int c=search(queries[i][1]);
if(c!=-1)
result.add(c);
}
}
}
int[] arr = new int[result.size()];
for(int i=0;i<result.size();i++)
arr[i] = result.get(i);
return arr;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int queriesRows = Integer.parseInt(scanner.nextLine().trim());
String[][] queries = new String[queriesRows][2];
for (int queriesRowItr = 0; queriesRowItr < queriesRows; queriesRowItr++) {
String[] queriesRowItems = scanner.nextLine().split(" ");
for (int queriesColumnItr = 0; queriesColumnItr < 2; queriesColumnItr++) {
String queriesItem = queriesRowItems[queriesColumnItr];
queries[queriesRowItr][queriesColumnItr] = queriesItem;
}
}
int[] result = contacts(queries);
for (int resultItr = 0; resultItr < result.length; resultItr++) {
bufferedWriter.write(String.valueOf(result[resultItr]));
if (resultItr != result.length - 1) {
bufferedWriter.write("\n");
}
}
bufferedWriter.newLine();
bufferedWriter.close();
}
}