25 · Tries

#208 Implement Trie (Prefix Tree)

Mediumsolve on LeetCode ↗

frequently reported atAppleOracleMicrosoftAmazonGoogleMeta

the problem

In plain words

Build a class Trie with insert(word), search(word) (is this exact word stored?) and startsWith(prefix) (is any stored word starting with prefix?).

in insert("apple"), search("apple"), search("app"), startsWith("app"), insert("app"), search("app")

out null, true, false, true, null, true

"app" is only a prefix until it is inserted itself.

  • 1 ≤ word.length, prefix.length ≤ 2000
  • Lowercase English letters only
  • At most 3·10⁴ calls in total

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #211
    2. #1268
    3. #648
      Replace Words
      Mediumin Tries