Java 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. Java // Java program to find longest common prefix of // given array of words. import java.util.*; public class GFG { public String longestCommonPrefix(String[] a) { int size = a.length; /* if size is 0, return empty string */ if (size == 0) return ""; if (size == 1) return a[0]; /* sort the array of strings */ Arrays.sort(a); /* find the minimum length from first and last string */ int end = Math.min(a[0].length(), a[size-1].length()); /* find the common prefix between the first and last string */ int i = 0; while (i < end && a[0].charAt(i) == a[size-1].charAt(i) ) i++; String pre = a[0].substring(0, i); return pre; } /* Driver Function to test other function */ public static void main(String[] args) { GFG gfg = new GFG(); String[] input = {"geeksforgeeks", "geeks", "geek", "geezer"}; System.out.println( "The longest Common Prefix is : " + gfg.longestCommonPrefix(input)); } } 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. Space Complexity: 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 Java Program To Find Longest Common Prefix Using Sorting kartik Follow Improve Article Tags : Strings Sorting Java Programs DSA Longest Common Prefix +1 More Practice Tags : SortingStrings Similar Reads Java 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 5 min read How to Find the Longest Common Prefix of Two Strings in Java? In this article, we will find the longest common prefix of two Strings in Java. Examples: Input: String 1= geeksforgeeks, String 2 = geezerOutput: âgeeâ Input: String 1= flower, String 2 = flightOutput: âflâ Methods to Find the Longest common Prefix of two Strings in JavaBelow are the methods by whi 4 min read Java 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 4 min read Java Program to Sort Names in an Alphabetical Order For, sorting names in an Alphabetical order there are multiple ways to sort the array, like using inbuilt Arrays.sort() method or using normal sorting algorithms like the bubble sort, merge sort. Here let's use the bubble sort and inbuilt sort. Example: Input : Array[] = {"Sourabh", "Anoop, "Harsh", 3 min read Java 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 8 min read Longest Prefix Matching - A Trie Based Solution in Java Given a dictionary of words and an input string, we need to find the longest prefix of the string that is also a word in the dictionary.Examples: Let the dictionary contain the following words:{are, area, base, cat, cater, children, basement}Below are the sample input/output:Input String Outputcater 4 min read Java Program for Check if given string can be formed by two other strings or their permutations Given a string str and an array of strings arr[], the task is to check if the given string can be formed by any of the string pair from the array or their permutations. Examples: Input: str = "amazon", arr[] = {"loa", "azo", "ft", "amn", "lka"} Output: Yes The chosen strings are "amn" and "azo" whic 4 min read Lexicographically smallest and largest substring of size k Given String str and an integer k, find the lexicographically smallest and largest substring of length kLexicography order, also called as alphabetical order or dictionary order, A < B <... < Y < Z < a < b <.. < y < z Examples: Input : String: hello Size: 2 Distinct Substr 5 min read C++ Program To Find Longest Common Prefix Using Sorting 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 2 min read Python Program To Find Longest Common Prefix Using Sorting 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 2 min read Like