Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6454 |

@blackshadows | 6380 |

@mb1973 | 5714 |

@Quandray | 5245 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5046 |

@sanjay05 | 3762 |

@kirtidee18 | 3709 |

@mantu_singh | 3556 |

@marius_valentin_dragoi | 3523 |

@sushant_a | 3459 |

Complete Leaderboard | |

Handle | Score |

@cfwong8 | 1285 |

@gurshehzadsingh | 1052 |

@pritsahkar2000 | 812 |

@satyamchhatrala | 804 |

@nithinreddy3210 | 801 |

@iib2019024 | 745 |

@mohanreddy8847 | 704 |

@iamchaitanyahegde | 662 |

@shafeeq3543 | 651 |

@workit | 646 |

@AkashLahoty | 642 |

Complete Leaderboard |

Given a string **S**, find the longest repeating non-overlapping substring in it. In other words find 2 identical substrings of maximum length which do not overlap. If there exists more than one such substring return any of them. Print the longest non-overlapping substring. If no such substring exists print -1.

**Note: **Multiple Answers are possible but you have to print the substring which occurs atleat twice first.

For Example: "ablhiabohi". Here both "ab" and "hi" are possible answers. But you will have to return "ab" as because it repeats before "hi".

**Example 1:**

Input:N =13S ="geeksforgeeks"Output:geeksExplanation:The string "geeks" is the longest Substring of S which is repeating but not overlapping.

**Example 2:**

Input:N =8S ="heyhheyi"Output:heyExplanation:The string "hey" is the longest Substring of S which is repeating but not overlapping.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function** longestSubstring()** which takes a Integer N and a String S as input and returns the answer.

**Expected Time Complexity:** O(N^{2})

**Expected Auxiliary Space:** O(N^{2})

**Constraints:**

1 <= N <= 10^{3}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Longest repeating and non-overlapping substring

...