Related Articles

Related Articles

Longest Common Substring in an Array of Strings
  • Difficulty Level : Easy
  • Last Updated : 21 Oct, 2020

We are given a list of words sharing a common stem i.e the words originate from same word for ex: the words sadness, sadly and sad all originate from the stem ‘sad’
Our task is to find and return the Longest Common Substring also known as stem of those words. In case there are ties, we choose the smallest one in alphabetical order. 

Examples: 

Input : grace graceful disgraceful gracefully
Output : grace

Input : sadness sad sadly
Output : sad

The idea is to take any word from the list as reference and form all its substrings and iterate over the entire list checking if the generated substring occurs in all of them.

Below is the implementation of the above idea:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the stem of given list of
// words
#include <bits/stdc++.h>
using namespace std;
 
// function to find the stem (longest common
// substring) from the string array
string findstem(vector<string> arr)
{
    // Determine size of the array
    int n = arr.size();
 
    // Take first word from array as reference
    string s = arr[0];
    int len = s.length();
 
    string res = "";
 
    for (int i = 0; i < len; i++) {
        for (int j = i + 1; j <= len; j++) {
            // generating all possible substrings
            // of our reference string arr[0] i.e s
            string stem = s.substr(i, j);
            int k = 1;
            for (k = 1; k < n; k++) {
                // Check if the generated stem is
                // common to all words
                if (arr[k].find(stem) == std::string::npos)
                    break;
            }
 
            // If current substring is present in
            // all strings and its length is greater
            // than current result
            if (k == n && res.length() < stem.length())
                res = stem;
        }
    }
 
    return res;
}
 
// Driver code
int main()
{
    vector<string> arr{ "grace", "graceful", "disgraceful",
                        "gracefully" };
 
    // Function call
    string stems = findstem(arr);
    cout << stems << endl;
}
 
// This code is contributed by
// sanjeev2552

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the stem of given list of
// words
import java.io.*;
import java.util.*;
 
class stem {
 
    // function to find the stem (longest common
    // substring) from the string  array
    public static String findstem(String arr[])
    {
        // Determine size of the array
        int n = arr.length;
 
        // Take first word from array as reference
        String s = arr[0];
        int len = s.length();
 
        String res = "";
 
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j <= len; j++) {
 
                // generating all possible substrings
                // of our reference string arr[0] i.e s
                String stem = s.substring(i, j);
                int k = 1;
                for (k = 1; k < n; k++)
 
                    // Check if the generated stem is
                    // common to all words
                    if (!arr[k].contains(stem))
                        break;
 
                // If current substring is present in
                // all strings and its length is greater
                // than current result
                if (k == n && res.length() < stem.length())
                    res = stem;
            }
        }
 
        return res;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        String arr[] = { "grace", "graceful",
                        "disgraceful","gracefully" };
       
        // Function call
        String stems = findstem(arr);
        System.out.println(stems);
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find the stem
# of given list of words
 
# function to find the stem (longest
# common substring) from the string array
 
 
def findstem(arr):
 
    # Determine size of the array
    n = len(arr)
 
    # Take first word from array
    # as reference
    s = arr[0]
    l = len(s)
 
    res = ""
 
    for i in range(l):
        for j in range(i + 1, l + 1):
 
            # generating all possible substrings
            # of our reference string arr[0] i.e s
            stem = s[i:j]
            k = 1
            for k in range(1, n):
 
                # Check if the generated stem is
                # common to all words
                if stem not in arr[k]:
                    break
 
            # If current substring is present in
            # all strings and its length is greater
            # than current result
            if (k + 1 == n and len(res) < len(stem)):
                res = stem
 
    return res
 
 
# Driver Code
if __name__ == "__main__":
 
    arr = ["grace", "graceful",
           "disgraceful", "gracefully"]
     
    # Function call
    stems = findstem(arr)
    print(stems)
 
# This code is contributed by ita_c

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the stem of given list of
// words
using System;
using System.Collections.Generic;
 
class stem
{
    // function to find the stem (longest common
    // substring) from the string array
    public static String findstem(String []arr)
    {
        // Determine size of the array
        int n = arr.Length;
 
        // Take first word from array as reference
        String s = arr[0];
        int len = s.Length;
 
        String res = "";
 
        for (int i = 0; i < len; i++)
        {
            for (int j = i + 1; j <= len; j++)
            {
 
                // generating all possible substrings
                // of our reference string arr[0] i.e s
                String stem = s.Substring(i, j-i);
                int k = 1;
                for (k = 1; k < n; k++)
 
                    // Check if the generated stem is
                    // common to all words
                    if (!arr[k].Contains(stem))
                        break;
                 
                // If current substring is present in
                // all strings and its length is greater
                // than current result
                if (k == n && res.Length < stem.Length)
                    res = stem;
            }
        }
 
        return res;
    }
 
    // Driver Code
    public static void Main(String []args)
    {
        String []arr = { "grace", "graceful", "disgraceful",
                                            "gracefully" };
        // Function call
        String stems = findstem(arr);
        Console.WriteLine(stems);
    }
}
 
// This code contributed by Rajput-Ji

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the stem of given list of
// words
 
    // function to find the stem (longest common
    // substring) from the string array
    function findstem($arr)
    {
        // Determine size of the array
        $n = count($arr);
 
        // Take first word from array as reference
        $s = $arr[0];
        $len = strlen($s);
         
        $res = "";
 
        for ($i = 0; $i < $len; $i++)
        {
            for ($j = $i+1; $j <=$len; $j++)
            {
 
                // generating all possible substrings
                // of our reference string arr[0] i.e s
                $stem = substr($s,$i, $j-$i);
                 
                $k = 1;
                for ($k = 1; $k < $n; $k++)
 
                    // Check if the generated stem is
                    // common to all words
                    if (!strpos($arr[$k],$stem))
                        break;
                 
                // If current substring is present in
                // all strings and its length is greater
                // than current result
                if ($k <= $n && strlen($res) < strlen($stem))
                    $res = $stem;
                 
            }
        }
 
        return $res;
    }
 
    // Driver Code
    $arr = array( "grace", "graceful",
                 "disgraceful", "gracefully" );
 
    // Function call
    $stems = findstem($arr);
    print($stems);
     
// This code is contributed by mits
?>

chevron_right


Output

grace

This article is contributed by DANISH KALEEM. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :