Dsa
Trie
Hard
The K-Diverse Prefix
A prefix is defined as 'K-diverse' if the set of words that start with this prefix end in at least K distinct characters. Find the length of the longest K-diverse prefix in a given set of words.
Input Format:
1. An integer K.
2. A sequence of space-separated strings.
Output Format: An integer representing the length of the longest K-diverse prefix.
Example:
Input: 2 apple apply ball
Output: 4
Explanation: The prefix 'appl' is shared by 'apple' (ends in 'e') and 'apply' (ends in 'y'). These are 2 distinct terminal characters. No longer prefix has 2 or more terminal characters.
Key concepts
character-diversitysubtree-stats
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now