Given n directory paths, we need to write a program to find longest common directory path.
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 .
Longest Common Path is /home/User/Desktop/gfg!
- Simplify the directory path (Unix like)
- Java program to merge contents of all the files in a directory
- Longest Common Substring | DP-29
- Longest Common Subsequence | DP-4
- Longest Common Prefix Matching | Set-6
- Longest Common Anagram Subsequence
- Longest Common Subsequence | DP using Memoization
- Printing Longest Common Subsequence
- Longest Common Prefix using Sorting
- LCS (Longest Common Subsequence) of three strings
- Longest Common Prefix using Trie
- Print the longest common substring
- Longest Common Extension / LCE | Set 2 ( Reduction to RMQ)
- SequenceMatcher in Python for Longest Common Substring
- Length of longest common subsequence containing vowels
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.