-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie-Dictionary
More file actions
90 lines (64 loc) · 1.76 KB
/
Trie-Dictionary
File metadata and controls
90 lines (64 loc) · 1.76 KB
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
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.*;
public class Main
{
static class TrieNode
{
HashMap < Character, TrieNode > map =
new HashMap < Character, TrieNode > ();
boolean isWord = false;
String mean;
}
static class Trie
{
TrieNode root = new TrieNode ();
public void addWord (String word, String mean)
{
TrieNode temp = root;
for (int i = 0; i < word.length (); i++)
{
char c = word.charAt (i);
if (!temp.map.containsKey (c))
{
temp.map.put (c, new TrieNode ());
}
temp = temp.map.get (c);
}
temp.isWord = true;
temp.mean = mean;
}
public String getMean (String word)
{
TrieNode temp = root;
for (int i = 0; i < word.length (); i++)
{
char c = word.charAt (i);
if (!temp.map.containsKey (c))
{
return "Word not found";
}
temp = temp.map.get (c);
}
if (temp.isWord)
return temp.mean;
else
return "Word not found";
}
}
public static void main (String[]args)
{
System.out.println ("Hello World");
Trie trie = new Trie ();
trie.addWord ("bat", "used to play cricket");
trie.addWord ("battle", "A big fight between enemies");
trie.addWord ("cat", "animal eats mouse");
System.out.println (trie.getMean ("bat"));
System.out.println (trie.getMean ("boy"));
System.out.println (trie.getMean ("battle"));
System.out.println (trie.getMean ("batt"));
}
}