Watch a trie insert words letter by letter, then search and check prefixes. Worked example with ap and apt, type your own words, and see O(L) time per step.
About this algorithm
A trie (prefix tree) stores strings one character per node, so words that begin the same way share the same first nodes. Each node keeps a map from a letter to a child and a flag that says whether a stored word finishes there. Insert, search and startsWith all walk down from the root one letter at a time, which is LeetCode 208, Implement Trie.
Pick a trie over a hash set when you need to ask about prefixes and not only whole words. A hash set answers "is apple stored?" in about the same time, but "does any stored word begin with app?" forces it to check every entry, while the trie answers by walking three nodes. Tries also suit spell checkers, word games and routing tables, where many keys share their beginnings. If you only ever look up whole words, a hash set is simpler and uses less memory. This page lets you run each operation by hand on a small dictionary. To list every word under a prefix, use the autocomplete page; to find what all the words share, use the longest common prefix page.
Start at the root. For each letter, move to the child for that letter, creating the child first if it is missing. After the last letter, set the node's end-of-word flag. Inserting a word that is already there, or one that is a prefix of a stored word, creates no nodes and only sets the flag.
Follow the same path. If a letter has no child, return false at once. If every letter is found, the answer is the end-of-word flag on the last node: true only when that exact word was inserted, false when the path exists only as the start of longer words.
Follow the path the same way and return false at the first missing letter. If the whole prefix is walked, return true without looking at the flag. A stored word counts as a prefix of itself, so startsWith on a whole word is also true.
The Reset button clears your changes and rebuilds the starting dictionary. There is no delete on this page.
The page starts with five words: app, apple, apply, ball and bat. They need 11 letter nodes under the root, which has two children, a and b. Pressing Play before running any operation searches apply, which walks a, p, p, l, y and finds the flag set, so it is found. Type ap and press Search: both letters exist, but the p node has no flag, so the result is false, ap is only a prefix. Press StartsWith on ap instead and the same walk returns true. StartsWith on cat fails on the first letter, since the root has no c child. Now insert apt: a and p already exist, t is missing under ap, so one node is created and flagged, 12 letter nodes in all, and a search for apt now succeeds. Inserting ap after that creates nothing and only flags the existing p node.
The box takes up to 12 characters. It lowercases what you type and drops anything that is not a letter from a to z before running, and pressing Enter runs Insert. Each operation acts on the trie the last one left until you press Reset.
Time: insert, search and startsWith each do one child lookup per letter, so they cost the length L of the word or prefix, no matter how many words are stored. Space: N words of length at most L need at most N × L nodes, fewer when they share prefixes, and each insert adds at most L new nodes.