Skip to main content
All Questions

Minimize the trie nodes needed after permuting each string's characters.

Unlock detailed company stats for this questionUpgrade

You are given an array of N strings words. Construct a prefix tree (trie) to store all N strings. Before inserting, you may independently permute the characters of each string in any order. Find the minimum total number of nodes (including the root) needed to represent all N strings after choosing optimal orderings.

Constraints:

  • 1 <= N <= 16
  • 1 <= length of words[i] <= 10^5
  • All strings consist of lowercase English letters.

Example: Input: N = 3, words = ["pqr", "qps", "ptr"] Output: 7 Explanation: permute the words to ["pqr", "pqs", "ptr"]. Root: 1 node; shared prefix 'p': 1 node; shared prefix 'pq' between words 1 and 2: 1 node; unique suffix nodes ('r', 's' and 'tr'): 4 nodes. Total = 7.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.