algoblazerEarly Access
LearnProblemsStudy GroupsMock InterviewLeaderboard
Log inSign up

All problems

‹ Back to map
1 shown · 0/287 solved

Minimum Trie Nodes After Reordering Letters

GoldCommunity Beta
Asked atMastercard
Solve problem →
4000ms256MBAdded Sep 25, 2026

You are given a list of lowercase strings words. You may reorder the letters inside each string however you like, and you choose the order for each string on its own.

The strings are then stored together in a prefix tree. A prefix tree is a rooted tree. Its root stands for the empty string, and every other node carries one letter, so each node spells out the string formed by the letters from the root down to that node.

Storing a string means walking the path that spells it and creating any node on that path that is missing. Two strings therefore share nodes for as long as their letters agree, and use separate nodes from the first position where they differ.

Return the smallest number of nodes the prefix tree can have, counting the root.

Examples

Example 1

Input: words = ["pqr","qps","ptr"]
Output: 7
Explanation: Reorder the second string to "pqs", which lets it share p and q with the first string. Below the root sits p, below p sit q and t, below q sit r and s, and below t sits r. With the root that is 7 nodes.
Read full statement →

Constraints

  • 1≤n≤161 \leq n \leq 161≤n≤16 where n=n =n= words.length
  • 1≤1 \leq1≤ words[i].length ≤105\leq 10^5≤105
  • words[i] consists of lowercase English letters

Details

Solved by1 person
Time limit4000ms
Memory256MB
AddedSep 25, 20263 hours ago