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.
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.
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 email@example.com. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Longest Common Substring | DP-29
- Print the longest common substring
- SequenceMatcher in Python for Longest Common Substring
- Longest common substring in binary representation of two numbers
- Longest Common Substring (Space optimized DP solution)
- Check if two strings have a common substring
- LCS (Longest Common Subsequence) of three strings
- Longest common anagram subsequence from N strings
- Find the longest string that can be made up of other strings from the array
- Number of common base strings for two strings
- Longest Common Subsequence | DP-4
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 2
- Longest Palindromic Substring | Set 1
- Longest substring of vowels
- Longest substring with count of 1s more than 0s
- Longest Common Subsequence | DP using Memoization
- Longest Common Anagram Subsequence
- Printing Longest Common Subsequence
- Longest Common Extension / LCE | Set 2 ( Reduction to RMQ)