Largest substring of str2 which is a prefix of str1
Given two string str1 and str2, the task is to find the longest prefix of str1 which is present as a substring of the string str2. Print the prefix if possible else print -1.
Examples:
Input: str1 = “geeksfor”, str2 = “forgeeks”
Output: geeks
All the prefixes of str1 which are present in str2
are “g”, “ge”, “gee”, “geek” and “geeks”.
Input: str1 = “abc”, str2 = “def”
Output: -1
Approach: Check whether str1 is present as a substring in str2. If yes then str1 is the required string else remove the last character from str1 and repeat these steps until either the string str1 becomes empty or the required string is found.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to return the largest substring // in str2 which is a prefix of str1 string findPrefix(string str1, string str2) { // To store the index in str2 which // matches the prefix in str1 int pos = -1; // While there are characters left in str1 while (!str1.empty()) { // If the prefix is not found in str2 if (str2.find(str1) == string::npos) // Remove the last character str1.pop_back(); else { // Prefix found pos = str2.find(str1); break ; } } // No substring found in str2 that // matches the prefix of str1 if (pos == -1) return "-1" ; return str1; } // Driver code int main() { string str1 = "geeksfor" ; string str2 = "forgeeks" ; cout << findPrefix(str1, str2); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the largest substring // in str2 which is a prefix of str1 static String findPrefix(String str1, String str2) { // To store the index in str2 which // matches the prefix in str1 boolean pos = false ; // While there are characters left in str1 while (str1.length() > 0 ) { // If the prefix is not found in str2 if (!str2.contains(str1)) // Remove the last character str1 = str1.substring( 0 , str1.length() - 1 ); else { // Prefix found pos = str2.contains(str1); break ; } } // No substring found in str2 that // matches the prefix of str1 if (pos == false ) return "-1" ; return str1; } // Driver code public static void main(String[] args) { String str1 = "geeksfor" ; String str2 = "forgeeks" ; System.out.println(findPrefix(str1, str2)); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approach import operator # Function to return the largest substring # in str2 which is a prefix of str1 def findPrefix(str1, str2): # To store the index in str2 which # matches the prefix in str1 pos = False ; # While there are characters left in str1 while ( len (str1) ! = 0 ): # If the prefix is not found in str2 if operator.contains(str2, str1) ! = True : # Remove the last character str1 = str1[ 0 : len (str1) - 1 ]; else : # Prefix found pos = operator.contains(str2, str1); break ; # No substring found in str2 that # matches the prefix of str1 if (pos = = False ): return "-1" ; return str1; # Driver code if __name__ = = '__main__' : str1 = "geeksfor" ; str2 = "forgeeks" ; print (findPrefix(str1, str2)); # This code is contributed by 29AjayKumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the largest substring // in str2 which is a prefix of str1 static String findPrefix(String str1, String str2) { // To store the index in str2 which // matches the prefix in str1 bool pos = false ; // While there are characters left in str1 while (str1.Length > 0) { // If the prefix is not found in str2 if (!str2.Contains(str1)) // Remove the last character str1 = str1.Substring(0, str1.Length - 1); else { // Prefix found pos = str2.Contains(str1); break ; } } // No substring found in str2 that // matches the prefix of str1 if (pos == false ) return "-1" ; return str1; } // Driver code public static void Main(String[] args) { String str1 = "geeksfor" ; String str2 = "forgeeks" ; Console.WriteLine(findPrefix(str1, str2)); } } // This code is contributed by Princi Singh |
geeks
Time Complexity: O(N * M) where N, M are the lengths of given strings.
Recommended Posts:
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Count of characters in str1 such that after deleting anyone of them str1 becomes str2
- Check whether str1 can be converted to str2 with the given operations
- Minimum cost to convert str1 to str2 with the given operations
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Largest substring with same Characters
- Lexicographically smallest and largest substring of size k
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Longest prefix which is also suffix
- Prefix to Infix Conversion
- Postfix to Prefix Conversion
- Prefix to Postfix Conversion
- Longest Common Prefix using Trie
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.