Password

Practice this coding question on Coding Gurukul.

Problem statement

An archive stores a lowercase verification string on each storage cartridge. Its recovery password must be visible at the start, at the end, and at another position inside the same string. The archivist needs the longest password supported by all three locations. Your task Find the longest nonempty string that is a prefix and a suffix of s and also occurs strictly inside s. An inside occurrence must start after the first character and end before the last character. Important details The three occurrences may overlap. The prefix and suffix alone are not enough: there must be a third occurrence satisfying both inside boundaries. Return the text of the longest candidate, not its length. If no candidate exists, print Just a legend. Worked example For s = abcabcabc, abc occurs at positions 1, 4 and 7, so it meets all three conditions. The longer border abcabc has only its prefix and suffix occurrences, so the answer is abc. For aaaa, the answer is aa: the occurrence at positions 2 through 3 is strictly inside, even though the occurrences overlap.

Input format

Read one nonempty lowercase English string s.

Output format

Print the longest valid password, or Just a legend if none exists.

Constraints

1 <= length of s <= 10000; s contains only a through z.

Sample input

abcabcabc

Sample output

abc

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…