Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Given a string S of size N, the duty is to take away the longest prefix of the string which has a minimum of one duplicate substring current in S.
Word: The duplicate substring can’t be the prefix itself
Examples:
Enter: S = “GeeksforGeeks”
Output: “forGeeks”
Clarification: The longest substring which has a replica is “Geeks”.
After deleting this the remaining string turns into “forGeeks”.Enter: S = “aaaaa”
Output: “a”
Clarification: Right here the longest prefix which has a replica substring is “aaaa”.
So after deleting this the remaining string is “a”.
Word that the entire string shouldn’t be chosen as a result of then the duplicate string is the prefix itself.
Strategy: The issue may be solved based mostly on the next thought:
Discover all of the characters which generally is a start line of a substring which is a replica of the prefix. Then from these factors discover the duplicate of the longest prefix and delete that prefix.
Comply with the illustration beneath for a greater understanding
Illustration:
For instance take S = “aaaaa”
The attainable beginning factors may be at indices 1, 2, 3, 4.
For index 1: The attainable duplicate of the prefix is “aaaa”.
It has size = 4For index 2: The attainable duplicate of the prefix is “aaa”.
It has size = 3For index 3: The attainable duplicate of the prefix is “aa”.
It has size = 2For index 4: The attainable duplicate of the prefix is “a”.
It has size = 1So take away the prefix of size 4. Due to this fact the string turns into “a”
Comply with the method talked about beneath to unravel the issue:
Beneath is the implementation of the above method:
|
Time Complexity: O(N2)
Auxiliary House: O(1)
[ad_2]