Design a data structure to store and manage a collection of strings, supporting two primary operations: adding a string to the repository and retrieving the lexicographically smallest prefix of a given length that exists in the repository. If no such prefix exists for the specified length, the retrieval operation should return an empty string.
Explanation: Both 'abce' and 'abcd' have a common prefix 'abc' of length 3, and 'abc' is lexicographically the smallest among all possible prefixes of length 3 in the repository.
Explanation: The prefix 'xy' of length 2 from the string 'xy' is the lexicographically smallest among all prefixes of length 2 in the repository.
Explanation: The single character 'p' is the shortest prefix and is lexicographically the smallest for the given length of 1.
Stack with O(1) minimum retrieval and Trie operations
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak