C# Coding Test Course, Dictionary Search

Problem Description

Given a list of words and a word to search for, the task is to check whether the search word is included in the word list. This problem can
be solved using a dictionary data structure. This can be easily implemented in C# using the Dictionary class.

Algorithm Problem

Problem


        Given a list of words words, write the following checkWords method. 
        
The checkWords method takes a list of strings and a string as parameters, and it should return true if the string is in the list, and false if it is not.
Example
Input: words = ["apple", "banana", "cherry"], searchWord = "banana"
Output: true

Solution

One of the most efficient methods to solve this problem is by using a hash table. By using a hash table to store the list of words
beforehand, we can find words with O(1) time complexity during searches. Utilizing C#’s Dictionary class allows
us to implement this, so we will solve the problem using the following steps.

Step 1: Convert the Word List to a Dictionary

First, we will convert the given word list into a Dictionary. Each word will be used as a key and the value will be
an unnecessary empty string.


        // C# code
        using System;
        using System.Collections.Generic;

        public class DictionarySearch
        {
            private Dictionary wordDictionary;

            public DictionarySearch(List words)
            {
                wordDictionary = new Dictionary();
                foreach (var word in words)
                {
                    wordDictionary[word] = true; // Value is not needed, set to true
                }
            }
        }
        

Step 2: Write the Word Search Method

Now, we will write the checkWords method. This method will check whether the word being searched exists in the dictionary.


        public bool CheckWords(string searchWord)
        {
            return wordDictionary.ContainsKey(searchWord);
        }
        

Step 3: Implement the Final Class and Method

We will combine the above steps to complete the class as a final solution.


        using System;
        using System.Collections.Generic;

        public class DictionarySearch
        {
            private Dictionary wordDictionary;

            public DictionarySearch(List words)
            {
                wordDictionary = new Dictionary();
                foreach (var word in words)
                {
                    wordDictionary[word] = true;
                }
            }

            public bool CheckWords(string searchWord)
            {
                return wordDictionary.ContainsKey(searchWord);
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                List words = new List { "apple", "banana", "cherry" };
                DictionarySearch dictionarySearch = new DictionarySearch(words);

                Console.WriteLine(dictionarySearch.CheckWords("banana")); // Output: true
                Console.WriteLine(dictionarySearch.CheckWords("grape")); // Output: false
            }
        }
        

Complexity Analysis

– Time complexity: O(1) – Using a hash table to store and search for words is optimally performed in O(1) time.
– Space complexity: O(n) – Proportional to the size n of the given word list.

Key Points

– Using the Dictionary class is key to solving the problem. It ensures fast search speeds.
– In string comparisons, the results may differ based on case sensitivity, so caution is needed.
– After running the overall code, it is necessary to validate it through various test cases.

Conclusion

In this tutorial, we explained how to solve the dictionary search problem using C#. We demonstrated that a data structure approach
utilizing a hash table can efficiently satisfy the problem’s given conditions. The use of such data structures is frequently encountered
in actual coding tests, so practicing in advance is important.