Given string str. The task is to find the longest sub-string which is a prefix, a suffix and a sub-string of the given string, str. If no such string exists then print -1.
Input: str = “geeksisforgeeksinplatformgeeks”
Input: str = “fixprefixsuffix”
Note: The Set-1 of this article is attached here.
- First we are calculating the Z array by using the Z algorithm.
- Update the values in Bit array by 1 from index z[i].
- Querying for the maximum length needed substring using the pref function.
- If len is 0 then such substring is not possible from the given string.
Below is the implementation approach:
Time complexity: O(N)
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.
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Print the longest prefix of the given string which is also the suffix of the same string
- Longest prefix which is also suffix
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Longest Palindrome in a String formed by concatenating its prefix and suffix
- First string from the given array whose reverse is also present in the same array
- Check if suffix and prefix of a string are palindromes
- String from prefix and suffix of given two strings
- Longest string in an array which matches with prefix of the given string
- Longest string which is prefix string of at least two strings
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Find length of longest subsequence of one string which is substring of another string
- Largest substring of str2 which is a prefix of str1
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Python | Check if a Substring is Present in a Given String
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Most frequent word in first String which is not present in second String
- Remove characters from the first string which are present in the second string
- Find the longest common prefix between two strings after performing swaps on second string
- Find if a given string can be represented from a substring by iterating the substring “n” times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.