A simple trie for a prefix match problem, by Daily Coding Problem

The daily coding problem for today was this one:

Good morning! Here's your coding interview problem for today.
This problem was asked by Twitter.
Implement an autocomplete system. That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix.
For example, given the query string de and the set of strings [dog, deer, deal], return [deer, deal].
Hint: Try preprocessing the dictionary into a more efficient data structure to speed up queries.

The problem is begging for you to use a Trie data structure. I've written about tries many years ago (you can search for "Trie" in the anothercasualcoder blog), but I thought of an even simpler implementation.
Honestly you only need two member variables for your Trie class:

  • A boolean that indicates whether this node is a leaf node (meaning it is the end of a word)
  • A hashtable of Tries corresponding to its children
Honestly, that's all. I find it much easier to implement everything recursively for the trie. I also believe that using a hashtable is more efficient than for instance allocating an array of Tries for each letter - the allocation is static and you might not need all that space after all since your Trie might end up being very sparse.
The code is below. It may look complicated - it isn't. If you follow the logic that:

  • Either the current node has a match to the first letter (and it may or may not be a leaf), in which case you'll call recursively for the rest of the word,
  • Or, there is no match and you'll bail
It becomes relatively simple. Code is in the infamous GitHub: https://github.com/marcelodebarros/dailycodingproblem/blob/master/DailyCodingProblem09252018.cs

Any questions, ping me. Cheers, Marcelo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace DailyCodingProblem
{
class DailyCodingProblem09252018
{
private SimpleTrie simpleTrie = new SimpleTrie();
public void CreateTrie(string[] listWords)
{
foreach (string word in listWords) simpleTrie.AddWord(word);
}

public void PrintMatches(string prefix)
{
List<string> matches = simpleTrie.MatchPrefixes(prefix);
foreach (string match in matches)
{
Console.WriteLine(match);
}
}
}

class SimpleTrie
{
public bool isLeaf = false;
private Hashtable children = null;

public SimpleTrie() { }

public SimpleTrie(bool isLeaf)
{
this.isLeaf = isLeaf;
}

public void AddWord(string word)
{
if (String.IsNullOrEmpty(word)) return;
if (children == null) children = new Hashtable();
SimpleTrie child = null;
if (!children.ContainsKey(word[0]))
{
child = new SimpleTrie(word.Length == 1);
children.Add(word[0], child);
}
child = (SimpleTrie)children[word[0]];
child.AddWord(word.Substring(1));
}

public bool IsWordPresent(string word)
{
if (String.IsNullOrEmpty(word) || children == null || !children.ContainsKey(word[0])) return false;
if (word.Length == 1 && ((SimpleTrie)children[word[0]]).isLeaf) return true;
return ((SimpleTrie)children[word[0]]).IsWordPresent(word.Substring(1));
}

public List<string> MatchPrefixes(string prefix)
{
List<string> matches = new List<string>();
_MatchPrefixes(prefix, "", ref matches);
return matches;
}

private void _MatchPrefixes(string prefix, string currentWord, ref List<string> matches)
{
if (String.IsNullOrEmpty(prefix))
{
_AllWords(currentWord, ref matches);
return;
}
if (children == null || !children.ContainsKey(prefix[0])) return;
((SimpleTrie)children[prefix[0]])._MatchPrefixes(prefix.Substring(1), currentWord + prefix[0].ToString(), ref matches);
}

private void _AllWords(string currentWord, ref List<string> matches)
{
if (isLeaf) matches.Add(currentWord);
if (children != null)
foreach (char letter in children.Keys)
((SimpleTrie)children[letter])._AllWords(currentWord + letter.ToString(), ref matches);
}
}
}

Comments

  1. Ideal problem to show the power of Tries :) BTW, the memory usage of Hashtable vs static array is a little tricky since hashtables are usually fairly large, but it's a safe choice since it would work for any alphabet whereas arrays would not make any sense for anything larger than ASCII.

    Reply Delete

Post a Comment

[フレーム]

Popular posts from this blog

Quasi FSM (Finite State Machine) problem + Vibe

Not really an FSM problem since the state isn't changing, it is just defined by the current input. Simply following the instructions should do it. Using VSCode IDE you can also engage the help of Cline or Copilot for a combo of coding and vibe coding, see below screenshot. Cheers, ACC. Process String with Special Operations I - LeetCode You are given a string  s  consisting of lowercase English letters and the special characters:  * ,  # , and  % . Build a new string  result  by processing  s  according to the following rules from left to right: If the letter is a  lowercase  English letter append it to  result . A  '*'   removes  the last character from  result , if it exists. A  '#'   duplicates  the current  result  and  appends  it to itself. A  '%'   reverses  the current  result . Return the final string  result  after processing all char...

Shortest Bridge – A BFS Story (with a Twist)

Here's another one from the Google 30 Days challenge on LeetCode — 934. Shortest Bridge . The goal? Given a 2D binary grid where two islands (groups of 1s) are separated by water (0s), flip the fewest number of 0s to 1s to connect them. Easy to describe. Sneaky to implement well. 🧭 My Approach My solution follows a two-phase Breadth-First Search (BFS) strategy: Find and mark one island : I start by scanning the grid until I find the first 1 , then use BFS to mark all connected land cells as 2 . I store their positions for later use. Bridge-building BFS : For each cell in the marked island, I run a BFS looking for the second island. Each BFS stops as soon as it hits a cell with value 1 . The minimum distance across all these searches gives the shortest bridge. πŸ” Code Snippet Here's the core logic simplified: public int ShortestBridge(int[][] grid) { // 1. Mark one island as '2' and gather its coordinates List<int> island = FindAndMark...

Classic Dynamic Programming IX

A bit of vibe code together with OpenAI O3. I asked O3 to just generate the sieve due to laziness. Sieve is used to calculate the first M primes (when I was using Miller-Rabin, was giving me TLE). The DP follows from that in a straightforward way: calculate the numbers from i..n-1, then n follows by calculating the min over all M primes. Notice that I made use of Goldbach's Conjecture as a way to optimize the code too. Goldbach's Conjecture estates that any even number greater than 2 is the sum of 2 primes. The conjecture is applied in the highlighted line. Cheers, ACC. PS: the prompt for the sieve was the following, again using Open AI O3 Advanced Reasoning: " give me a sieve to find the first M prime numbers in C#. The code should produce a List<int> with the first M primes " Minimum Number of Primes to Sum to Target - LeetCode You are given two integers  n  and  m . You have to select a multiset of  prime numbers  from the  first   m  pri...