Problem Description
You have a dictionary consisting of several words. You need to create a program that checks if a specific word exists in this dictionary
and outputs the meaning of that word.
The input consists of a list of words in the dictionary and a word entered by the user. If the word entered by the user is
not in the dictionary, it should output the message “The word could not be found.”
Input Details
- The first line contains an integer
N
(1 ≤ N ≤ 100,000
). - The next
N
lines contain the words in the dictionary. - The last line contains the word that the user wants to search for.
Output Details
Output the meaning of the word entered by the user, but if the word is not found in the dictionary, output “The word could not be found.”
Examples
Input: 5 apple banana grape orange pear grape Output: "grape": grape
Input: 5 apple banana grape orange pear watermelon Output: The word could not be found.
Problem Solving Strategy
This problem requires searching for a word, so choosing an efficient data structure is key.
Using a HashMap allows you to search for a word with an average time complexity of O(1).
Therefore, you can store the dictionary in a HashMap, then search for the word entered by the user in that HashMap
to output its meaning.
Java Code Implementation
Below is the Java code to solve the above problem. This code implements the dictionary using a HashMap and handles user input.
import java.util.HashMap; import java.util.Scanner; public class DictionarySearch { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input the number of words in the dictionary int N = scanner.nextInt(); scanner.nextLine(); // Remove the newline character // Initialize the HashMap HashMapdictionary = new HashMap<>(); // Input words and meanings for the dictionary for (int i = 0; i < N; i++) { String word = scanner.nextLine(); // Input word String meaning = scanner.nextLine(); // Input meaning dictionary.put(word, meaning); } // Input the word to search String searchWord = scanner.nextLine(); // Search for the word and output the result if (dictionary.containsKey(searchWord)) { System.out.println("\"" + searchWord + "\": " + dictionary.get(searchWord)); } else { System.out.println("The word could not be found."); } scanner.close(); } }
Code Explanation
1. The Scanner
class is used to handle user input, and HashMap
is used to store the dictionary.
2. First, the size of the dictionary N
is input, and then words and their meanings are added to the HashMap for N
times.
3. Next, the user inputs the word they want to search for, and it checks if that word is in the HashMap.
4. If the word exists, its meaning is printed; if not, the message "The word could not be found." is printed.
Complexity Analysis
- Time Complexity: O(1) for adding a word to the HashMap and O(1) for searching. Overall O(N).
- Space Complexity: O(N) as the HashMap stores N
words and their meanings.
Conclusion
In this article, we looked at an algorithm problem to find a specific word in a dictionary using Java.
We learned how to efficiently search for words using a HashMap and gained insights into what approach to take
during coding tests through the problem-solving process.
I hope you can confidently solve similar problems in future coding tests.