Program for longest common directory path

Given n directory paths, we need to write a program to find longest common directory path.


Input : 3

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

Input : 4

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 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 initializd
    // 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 =
                                    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 chracters 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;
    // 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] = {
    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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.