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)
- 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
- Find length of longest subsequence of one string which is substring of another string
- Find the longest common prefix between two strings after performing swaps on second string
- Find the longest substring with k unique characters in a given string
- String from prefix and suffix of given two strings
- Check if suffix and prefix of a string are palindromes
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Python | Check if a Substring is Present in a Given String
- Longest string in an array which matches with prefix of the given string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Longest prefix in a string with highest frequency
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Longest substring of 0s in a string formed by k concatenations
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.