C++ Program To Find Longest Common Prefix Using Sorting Last Updated : 23 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Problem Statement: Given a set of strings, find the longest common prefix.Examples: Input: {"geeksforgeeks", "geeks", "geek", "geezer"} Output: "gee" Input: {"apple", "ape", "april"} Output: "ap" The longest common prefix for an array of strings is the common prefix between 2 most dissimilar strings. For example, in the given array {"apple", "ape", "zebra"}, there is no common prefix because the 2 most dissimilar strings of the array "ape" and "zebra" do not share any starting characters. We have discussed five different approaches in below posts. Word by Word MatchingCharacter by Character MatchingDivide and ConquerBinary Search.Using Trie) In this post a new method based on sorting is discussed. The idea is to sort the array of strings and find the common prefix of the first and last string of the sorted array. C++ // C++ program to find longest common prefix // of given array of words. #include<iostream> #include<algorithm> using namespace std; // Function to find the longest common prefix string longestCommonPrefix(string ar[], int n) { // If size is 0, return empty string if (n == 0) return ""; if (n == 1) return ar[0]; // Sort the given array sort(ar, ar + n); // Find the minimum length from // first and last string int en = min(ar[0].size(), ar[n - 1].size()); // Now the common prefix in first and // last string is the longest common prefix string first = ar[0], last = ar[n - 1]; int i = 0; while (i < en && first[i] == last[i]) i++; string pre = first.substr(0, i); return pre; } // Driver Code int main() { string ar[] = {"geeksforgeeks", "geeks", "geek", "geezer"}; int n = sizeof(ar) / sizeof(ar[0]); cout << "The longest common prefix is: " << longestCommonPrefix(ar, n); return 0; } // This code is contributed by jrolofmeister OutputThe longest common prefix is: gee Time Complexity: O(MAX * n * log n ) where n is the number of strings in the array and MAX is maximum number of characters in any string. Please note that comparison of two strings would take at most O(MAX) time and for sorting n strings, we would need O(MAX * n * log n ) time. Auxiliary Space: O(1) as no extra space has been used. Please refer complete article on Longest Common Prefix using Sorting for more details! Comment More infoAdvertise with us Next Article C++ Program To Find Longest Common Prefix Using Sorting kartik Follow Improve Article Tags : Strings Sorting C++ Programs DSA Longest Common Prefix +1 More Practice Tags : SortingStrings Similar Reads C++ Program To Find Longest Common Prefix Using Word By Word Matching Given a set of strings, find the longest common prefix. Examples: Input : {âgeeksforgeeksâ, âgeeksâ, âgeekâ, âgeezerâ} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap" Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. We start with an example. Suppose 3 min read C++ Program for Longest Common Subsequence LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them. A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. For example, "abc", "abg", "bdf", "aeg", '"acefg", .. etc are subsequences of "abcdefg". So 3 min read C++ Program To Find Length Of The Longest Substring Without Repeating Characters Given a string str, find the length of the longest substring without repeating characters. For âABDEFGABEFâ, the longest substring are âBDEFGAâ and "DEFGAB", with length 6.For âBBBBâ the longest substring is âBâ, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below 6 min read C++ Program to Sort String of Characters Sorting a string means rearranging the characters of the given string in some defined order such as alphabetical order. In this article, we will learn how to sort a string by characters in C++.ExamplesInput: str = "geeksforgeeks"Output: "eeeefggkkorss"Explanation: The characters in the string are so 4 min read Longest prefix in a string with highest frequency Given a string, find a prefix with the highest frequency. If two prefixes have the same frequency then consider the one with the maximum length. Examples: Input : str = "abc" Output : abc Each prefix has same frequency(one) and the prefix with maximum length is "abc". Input : str = "abcab" Output : 7 min read Like