
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find shortest unique prefix for every word in a given list in C++
In this problem, we are given an array of words arr[]. Our task is to find the shortest unique prefix for every word in the given list.
Let’s take an example to understand the problem,
Input
arr[] = {“learn”, “programming”, “code”}
Output
c leap lear p
Solution Approach
A simple solution to the problem is by finding all the prefixes of the word. And then check if it is a prefix of any other word in the array. If it is not, print it.
An efficient approach is to use the trie data structure. We will construct a trie and store all words. Then find the frequency of visiting for each word while inserting.using the words, we will find its path to root which is the prefix. We will print all prefixes starting from the node with frequency 1.
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; #define MAX 256 struct trieNode { struct trieNode *child[MAX]; int freq; }; struct trieNode *newTrieNode(void){ struct trieNode *newNode = new trieNode; newNode->freq = 1; for (int i = 0; i<MAX; i++) newNode->child[i] = NULL; return newNode; } void insert(struct trieNode *root, string str) { int len = str.length(); struct trieNode *pCrawl = root; for (int level = 0; level<len; level++) { int index = str[level]; if (!pCrawl->child[index]) pCrawl->child[index] = newTrieNode(); else (pCrawl->child[index]->freq)++; pCrawl = pCrawl->child[index]; } } void findShortestUniquePrefixRec(struct trieNode *root, char prefixChar[], int ind) { if (root == NULL) return; if (root->freq == 1) { prefixChar[ind] = '\0'; cout<<prefixChar<<endl; return; } for (int i=0; i<MAX; i++) { if (root->child[i] != NULL) { prefixChar[ind] = i; findShortestUniquePrefixRec(root->child[i], prefixChar, ind+1); } } } void findShortestUniquePrefix(string arr[], int n) { struct trieNode *root = newTrieNode(); root->freq = 0; for (int i = 0; i<n; i++) insert(root, arr[i]); char prefixChar[250]; findShortestUniquePrefixRec(root, prefixChar, 0); } int main() { string arr[] = {"learn", "programming", "code", "leap"}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"All Shortest unique prefix for every words in a given list are : \n"; findShortestUniquePrefix(arr, n); return 0; }
Output
All Shortest unique prefix for every words in a given list are − c leap lear p
- Related Articles
- Shortest Completing Word in Python
- Finding shortest word in a string in JavaScript
- Maximum prefix-sum for a given range in C++
- Shortest Word Distance II in C++
- Shortest Word Distance III in C++
- Program to find length longest prefix sequence of a word array in Python
- Word Ladder (Length of shortest chain to reach a target word) in C++
- How to find strings with a given prefix in MySQL?
- Write a program in Python to filter City column elements by removing the unique prefix in a given dataframe
- Find Sum of all unique subarray sum for a given array in C++
- Find maximum of minimum for every window size in a given array in C++
- C++ program to find out the shortest cost path in a given graph for q queries
- Find frequency of given character at every position in list of lists in Python
- Program to check every sublist in a list containing at least one unique element in Python
- Minimum Unique Word Abbreviation in C++

Advertisements