GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — programming/problems/string/WordMeaningDictionary.java at master · rp-code9/programming

Expand file tree

/

WordMeaningDictionary.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
84 lines (72 loc) · 2.48 KB
/

WordMeaningDictionary.java

Copy path

File metadata and controls

84 lines (72 loc) · 2.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Problem : https://www.geeksforgeeks.org/implement-a-dictionary-using-trie/?ref=rp
*/
import java.util.HashMap;
public class WordMeaningDictionary {
private Node root;
// Node represent basic node of trie
// Use hashmap to store next pointers
private class Node {
String meaning;
HashMap<Character, Node> next;
public Node() {
meaning = null;
next = new HashMap<>();
}
}
public WordMeaningDictionary() {
root = new Node();
}
// If there no child of this node
public boolean isTerminalNode(Node node) {
return node.next.isEmpty();
}
// Insert the key and meaning of this key
public void insert(String key, String meaning) {
if (key == null || meaning == null) {
throw new IllegalArgumentException();
}
Node temp = root;
for (int i = 0; i < key.length(); ++i) {
char c = key.charAt(i);
if (temp.next.get(c) == null) {
temp.next.put(c, new Node());
}
temp = (Node)temp.next.get(c);
}
temp.meaning = meaning;
}
// Search for key and fetch the meaning otherwise return null
public String meaning(String key) {
if (key == null) {
throw new IllegalArgumentException();
}
Node temp = root;
for (int i = 0; i < key.length(); ++i) {
char c = key.charAt(i);
if (temp.next.get(c) == null) {
return null;
}
temp = (Node)temp.next.get(c);
}
return temp.meaning;
}
public static void main(String[] args) {
String[] words = {"language", "the method of human communication",
"computer", "A computer is a machine that can be instructed to carry out sequences of arithmetic or",
"map", "a diagrammatic representation of an area",
"book", "a written or printed work consisting of pages glued",
"science", "study of nature"};
WordMeaningDictionary dict = new WordMeaningDictionary();
for (int i = 0; i < words.length; i = i + 2) {
dict.insert(words[i], words[i + 1]);
}
String word = "book";
String meaning = dict.meaning(word);
if (meaning != null) {
System.out.println(meaning);
} else {
System.out.println("No meaning for " + word);
}
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.