Program for longest common directory path

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 .
[sourcecode language=”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 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 =
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 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;
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;
}

[/sourcecode]

Output:

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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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 contribute@geeksforgeeks.org to report any issue with the above content.