Longest Common Prefix Matching | Set-6
Last Updated :
25 Apr, 2023
Given a set of strings, find the longest common prefix.
Examples:
Input: str[] = {geeksforgeeks, geeks, geek, geezer}
Output: gee
Input: str[] = {apple, ape, april}
Output: ap
Previous Approaches: Set1 | Set2 | Set3 | Set4 | Set5
Approach:
- Sort the given set of N strings.
- Compare the first and last string in the sorted array of strings.
- The string with prefix characters matching in the first and last string will be the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string commonPrefixUtil(string str1, string str2)
{
string result;
int n1 = str1.length(), n2 = str2.length();
for ( int i = 0, j = 0; i <= n1 - 1 && j <= n2 - 1; i++, j++) {
if (str1[i] != str2[j])
break ;
result.push_back(str1[i]);
}
return (result);
}
void commonPrefix(string arr[], int n)
{
sort(arr, arr + n);
cout << commonPrefixUtil(arr[0], arr[n - 1]);
}
int main()
{
string arr[] = { "geeksforgeeks" , "geeks" ,
"geek" , "geezer" };
int n = sizeof (arr) / sizeof (arr[0]);
commonPrefix(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static String commonPrefixUtil(String str1, String str2) {
String result = "" ;
int n1 = str1.length(), n2 = str2.length();
for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i++, j++) {
if (str1.charAt(i) != str2.charAt(j)) {
break ;
}
result += str1.charAt(i);
}
return (result);
}
static void commonPrefix(String arr[], int n) {
Arrays.sort(arr);
System.out.println(commonPrefixUtil(arr[ 0 ], arr[n - 1 ]));
}
public static void main(String[] args) {
String arr[] = { "geeksforgeeks" , "geeks" ,
"geek" , "geezer" };
int n = arr.length;
commonPrefix(arr, n);
}
}
|
Python3
def commonPrefixUtil(str1, str2):
n1 = len (str1)
n2 = len (str2)
result = ""
j = 0
i = 0
while (i < = n1 - 1 and j < = n2 - 1 ):
if (str1[i] ! = str2[j]):
break
result + = (str1[i])
i + = 1
j + = 1
return (result)
def commonPrefix(arr, n):
arr.sort(reverse = False )
print (commonPrefixUtil(arr[ 0 ], arr[n - 1 ]))
if __name__ = = '__main__' :
arr = [ "geeksforgeeks" , "geeks" ,
"geek" , "geezer" ]
n = len (arr)
commonPrefix(arr, n)
|
C#
using System;
class GFG
{
static String commonPrefixUtil(String str1,
String str2)
{
string result = "" ;
int n1 = str1.Length, n2 = str2.Length;
for ( int i = 0, j = 0;
i <= n1 - 1 && j <= n2 - 1; i++, j++)
{
if (str1[i] != str2[j])
break ;
result += (str1[i]);
}
return (result);
}
static void commonPrefix(String []arr, int n)
{
Array.Sort(arr);
Console.Write(commonPrefixUtil(arr[0],
arr[n - 1]));
}
public static void Main()
{
String []arr = { "geeksforgeeks" , "geeks" ,
"geek" , "geezer" };
int n = arr.Length;
commonPrefix(arr, n);
}
}
|
PHP
<?php
function commonPrefixUtil( $str1 , $str2 )
{
$result = "" ;
$n1 = strlen ( $str1 );
$n2 = strlen ( $str2 );
for ( $i = 0, $j = 0; $i <= $n1 - 1 &&
$j <= $n2 - 1; $i ++, $j ++)
{
if ( $str1 [ $i ] != $str2 [ $j ])
break ;
$result = $result . $str1 [ $i ];
}
return ( $result );
}
function commonPrefix(& $arr , $n )
{
sort( $arr );
echo commonPrefixUtil( $arr [0], $arr [ $n - 1]);
}
$arr = array ( "geeksforgeeks" , "geeks" ,
"geek" , "geezer" );
$n = sizeof( $arr );
commonPrefix( $arr , $n );
?>
|
Javascript
<script>
function commonPrefixUtil(str1,str2)
{
let result = "" ;
let n1 = str1.length, n2 = str2.length;
for (let i = 0, j = 0; i <= n1 - 1 && j <= n2 - 1; i++, j++) {
if (str1[i] != str2[j]) {
break ;
}
result += str1[i];
}
return (result);
}
function commonPrefix(arr,n)
{
arr.sort();
document.write(commonPrefixUtil(arr[0], arr[n - 1])+ "<br>" );
}
let arr=[ "geeksforgeeks" , "geeks" ,
"geek" , "geezer" ];
let n = arr.length;
commonPrefix(arr, n);
</script>
|
Time Complexity: O(N * log N)
The space complexity of the given program is O(M), where M is the length of the longest string in the array. This is because the program only requires additional space to store the result string, which can be at most M characters long. The rest of the program does not use any significant amount of extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...