Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Shortest Unique Prefix Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Shortest Unique Prefix

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

You are given a list of words. You need to find the shortest prefix of each word so that it can identify the word uniquely.

Naive Approach

In the naive approach, we can try all the possible prefixes of all the words and check which prefix of the given word can act as its shortest unique prefix. In this way, we can generate the list of shortest unique prefixes of all the words.

Analysis

  • Time Complexity: O(n3)
  • Space Complexity: O(n)

Optimal Approach

In the optimal approach, we will use the "Trie" data structure. We will make a Trie of all the characters from the given list of strings, and also store the frequency of each character being inserted into the “Trie”. Now the prefix of each string will be the string, which is up to the character closest to the root and having a frequency of one. This way, using a DFS on the Trie, we can form our required shortest common prefixes.

Analysis

  • Time Complexity:  O(n) 
  • Space Complexity: O(n)

Implementation

C++

class TrieNode{
	public:
	vector<TrieNode*> characters;
	vector<int> count;
	bool isEndOfWord;
	TrieNode(){
	for(int i = 0; i < 26; i++){
		count.push_back(0);
		characters.push_back(NULL);
	}
		isEndOfWord = false;
	}
};
void insert(string word, TrieNode* root){
	TrieNode* node = root;
	for(int i = 0; i < word.size(); i++) {
		if(node->characters[word[i] - 'a'] != NULL) {
			node->count[word[i] - 'a'] += 1;
			node = node->characters[word[i] - 'a'];
		} else{
			TrieNode* newNode = new TrieNode();
			newNode->isEndOfWord = false;
			node->characters[word[i] - 'a'] = newNode;
			node->count[word[i] - 'a'] += 1;
			node = newNode;
		}
	}
}
string find(string word, TrieNode* root){
	TrieNode* node = root;
	string answer = "";
	for(int i = 0; i < word.size(); i++){
		if(node->count[word[i] - 'a'] == 1){
			answer += word[i];
			return answer;
		}
		else{
			answer += word[i];
			node = node->characters[word[i] - 'a'];
		}
	}
	return answer;
}
vector<string> getShortestUniquePrefixes(vector<string> &words) {
	TrieNode* root = new TrieNode();
	for(int i = 0; i < words.size(); i++) {
		insert(words[i], root);
	}
	vector<string> answer (words.size());
	for(int i = 0; i < words.size(); i++) {
		answer[i] = find(words[i], root);
	}
	return answer;
}

Java

class Solution {
	public static class TrieNode{
		TrieNode[] characters = new TrieNode[26];
		int[] count = new int[26];
		boolean isEndOfWord;
		TrieNode(){
			for(int i = 0; i < 26; i++){
				count[i] = 0;
				characters[i] = null;
			}
			isEndOfWord = false;
		}
	}
	void insert(String word, TrieNode root){
		TrieNode node = root;
		for(int i = 0; i < word.length(); i++) {
			if(node.characters[word.charAt(i) - 'a'] != null) {
				node.count[word.charAt(i) - 'a'] += 1;
				node = node.characters[word.charAt(i) - 'a'];
			}
			else{
				TrieNode newNode = new TrieNode();
				newNode.isEndOfWord = false;
				node.characters[word.charAt(i) - 'a'] = newNode;
				node.count[word.charAt(i) - 'a'] += 1;
				node = newNode;
			}
		}
	}
	String find(String word, TrieNode root){
		TrieNode node = root;
		String answer = "";
		for(int i = 0;i < word.length(); i++){
			if(node.count[word.charAt(i) - 'a'] == 1){
				answer += word.charAt(i);
				return answer;
			}
			else{
				answer += word.charAt(i);
				node = node.characters[word.charAt(i) - 'a'];
			}
		}
		return answer;
	}
	String[] getShortestUniquePrefixes(String[] words) {
		TrieNode root = new TrieNode();
		for(int i = 0; i < words.length; i++) {
			insert(words[i], root);
		}
		String[] answer = new String[words.length];
		for(int i = 0; i < words.length; i++) {
			answer[i] = find(words[i], root);
		}
		return answer;
	}
	
}
Related Content
Restaurant Reviews
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet