Program for longest common directory path - GeeksforGeeks

30 Jun, 2021
Given n directory paths, we need to write a program to find longest common directory path. Examples:  
Input : 3
"/home/User/Desktop/gfg/test"
"/home/User/Desktop/gfg/file"
"/home/User/Desktop/geeks/folders"

Output : Longest Common Path is /home/User/Desktop!

Input : 4
"/home/User/Desktop/gfg/test",
"/home/User/Desktop/gfg/file",
"/home/User/Desktop/gfg/folders",
"/home/User/Desktop/gfg/folders"


Output : Longest Common Path is /home/User/Desktop/gfg!
 
In the function LongestCommonPath() there are two main variables used which are “CommonCharacters” for storing the length of common characters and “CommonString” for storing the longest string of paths so that we get our longest common path. CommonCharacter variable gets updated in each iteration depending upon the length of common path appearing in the input paths. Lastly, we get the length of common path string after the iteration. Then, we get our common path by the substring till the separator ‘/’ as if we don’t do this then we get the longest common string among the paths, which should not be our result . Below is the implementation of the above approach .  
CPP
// CPP program for longest common path#include <bits/stdc++.h>using namespace std; string LongestCommonPath(const vector<string>& input_directory,                                                char separator){    vector<string>::const_iterator iter;     // stores the length of common characters    // and initialized with the length of    // first string .    int CommonCharacters = input_directory[0].length();     // stores the common string and initialized    // with the first string .    string CommonString = input_directory[0];     for (iter = input_directory.begin() + 1;        iter != input_directory.end(); iter++) {         // finding the two pointers through the mismatch()        // function which is 'mismatch at first string' and        //  'mismatch at the other string '        pair<string::const_iterator, string::const_iterator> p =                                mismatch(CommonString.begin(),                                    CommonString.end(), iter->begin());         // As the first mismatch string pointer points to the mismatch        // present in the "CommonString" so "( p.first-CommonString.begin())"        // determines the common characters in each iteration .        if ((p.first - CommonString.begin()) < CommonCharacters) {             // If the expression is smaller than commonCharacters            // then we will update the commonCharacters because            // this states that common path is smaller than the            // string commonCharacters we have evaluated till .            CommonCharacters = p.first - CommonString.begin();        }         // Updating CommonString variable        // so that in this maximum length        // string is stored .        if (*iter > CommonString)            CommonString = *iter;    }     // In 'found' variable we store the index of '/' in    // the length of common characters in CommonString    int found;     for (int i = CommonCharacters; i >= 0; --i) {        if (CommonString[i] == '/') {            found = i;            break;        }    }     // The substring from index 0 to index of    // separator is the longest common substring    return CommonString.substr(0, found);} int main(){    int n = 4;     string input_directory[4] = {        "/home/User/Desktop/gfg/test",        "/home/User/Desktop/gfg/file",        "/home/User/Desktop/gfg/folders",        "/home/User/Desktop/gfg/folders"    };     vector<string> input(input_directory,                        input_directory + n);    cout << "Longest Common Path is "        << LongestCommonPath(input, '/') << "!\n";     return 0;}
Longest Common Path is /home/User/Desktop/gfg!
 
References : Rosetta code 
as5853535
Strings
Technical Scripter
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Print all the duplicates in the input string
Vigenère Cipher
String class in Java | Set 1
sprintf() in C
Print all subsequences of a string
Convert character array to string in C++
How to Append a Character to a String in C
Program to count occurrence of a given character in a string
Naive algorithm for Pattern Searching