Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
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.
Examples:
Input: str = “geeksisforgeeksinplatformgeeks”
Output: geeks
Input: str = “fixprefixsuffix”
Output: fix
Note: The Set-1 of this article is attached here.
Approach:
The implementation is using BIT and Z algorithm. Learn more about them from this and this.
- 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:
// C++ program to find that // substring which is its // suffix prefix and also // found somewhere in betweem #include <bits/stdc++.h> using namespace std; // Z-algorithm function vector<int> z_function(string s) { int n = s.size(); vector<int> z(n); for (int i = 1, l = 0, r = 0; i < n; i++) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } int n, len = 0; // BIT array int bit[1000005]; string s; vector<int> z; map<int, int> m; // bit update function which // updates values from index // "idx" to last by value "val" void update(int idx, int val) { if (idx == 0) return; while (idx <= n) { bit[idx] += val; idx += (idx & -idx); } } // Query function in bit int pref(int idx) { int ans = 0; while (idx > 0) { ans += bit[idx]; idx -= (idx & -idx); } return ans; } // Driver Code int main() { s = "geeksisforgeeksinplatformgeeks"; n = s.size(); // Making the z array z = z_function(s); // update in the bit array from // index z[i] by increment of 1 for (int i = 1; i < n; i++) { update(z[i], 1); } for (int i = n - 1; i > 1; i--) { // if the value in z[i] is // not equal to (n-i) then no // need to move further if (z[i] != (n - i)) continue; // queryng for the maximum // length substring from // bit array if (pref(n) - pref(z[i] - 1) >= 2) { len = max(len, z[i]); } } if (!len) cout << "-1"; else cout << s.substr(0, len); return 0; } |
geeks
Time complexity: O(N)
Recommended Posts:
- 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 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.



