Shortest Unique Prefix Problem
You are given an array containing N
words. Your task is to derive the shortest prefix for each word that uniquely identifies it amongst the other words. For instance, given the words "abcd" and "abdc", both share the prefix "ab", which isn't unique. Thus, the prefixes "abc" and "abd" would uniquely identify "abcd" and "abdc", respectively.
Example:
Input:
T = 1
N = 2
words = ["abcd", "abdc"]
Output:
["abc", "abd"]
Constraints:
1 <= T <= 50
1 <= N <= 10^4
- The sum of the lengths of all words in a test case is less than 10^4.
- Time Limit: 1 sec
Note:
words
are assumed to be unique, thus ensuring that a unique prefix can always be found for each word.

AnswerBot
4mo
Given an array of words, find the shortest unique prefix for each word.
Create a Trie data structure to efficiently store and search for prefixes.
Traverse the Trie to find the shortest unique prefix fo...read more
Help your peers!
Add answer anonymously...
Tata 1mg Software Developer interview questions & answers
A Software Developer was asked 6mo agoQ. Explain Red Black Tree.
A Software Developer was asked 6mo agoQ. Given a 1-D array, how would you find the local minima?
A Software Developer was asked 11mo agoQ. How would you align two images on a page to have the same width and height?
Popular interview questions of Software Developer
A Software Developer was asked 6mo agoQ1. Explain Red Black Tree.
A Software Developer was asked 11mo agoQ2. How would you align two images on a page to have the same width and height?
A Software Developer was asked Q3. Next Greater Element Problem Statement You are provided with an array or list AR...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

