Bob's Hidden Password

medium strings string-matching 1000 ms · 256 MB

Bob's Hidden Password

Alice invented a game: she writes down a long string s and claims that a password is buried inside it. According to her rules, a string t qualifies as a password of s if

  • t is a prefix of s,
  • t is also a suffix of s, and
  • t occurs somewhere else in s too — at an occurrence that neither starts at the very first character of s nor ends at the very last character of s.

(The second and third conditions can be satisfied by the same occurrence: t just has to show up at least once strictly inside s, away from both ends.) Alice always picks the longest string that qualifies, and bets Bob he can't find a longer one. Bob suspects that sometimes no non-empty string qualifies at all — in which case he calls Alice's password "just a legend" and refuses to play.

Help Bob find the password, or confirm that it doesn't exist.

Formally, s has length n. Among all non-empty strings t with |t| < n such that t is a prefix of s, t is a suffix of s, and there exist indices 1 ≤ i ≤ j ≤ n with i > 1, j < n, j - i + 1 = |t|, and s[i..j] = t, find one of maximum length. (Note |t| < n is automatic: a password can't be the whole string, since then it would have nowhere strictly inside to hide.)

Input

The first line contains the integer n (1 ≤ n ≤ 10^6).

The second line contains the string s, of length n, consisting of lowercase English letters.

Output

Print the longest password of s, or Just a legend if no non-empty string satisfies all three conditions.

Notes

In the first example, s = ababab. The string abab is a prefix and a suffix, but its only other possible occurrence would have to end at the last character, so it doesn't count. The shorter border ab does occur strictly inside, starting at the third character, so it is the answer.

In the second example every letter of s is distinct, so the only common prefix/suffix is the empty string, and the answer is Just a legend.

In the third example, s = aabaa. The border aa is a prefix and a suffix, but its only two occurrences are the prefix itself and the suffix itself — neither is strictly inside. The shorter border a does occur strictly inside (the third character), so it is the answer.

Example 1

Input

6
ababab

Output

ab

Example 2

Input

5
abcde

Output

Just a legend

Example 3

Input

5
aabaa

Output

a

Hints · reveal one at a time if you're stuck

Hint 1

The candidates are exactly the borders of s, meaning the strings that are both a prefix and a suffix. Can you list every border length in a compact way without checking strings directly?

Hint 2

A border of length k has a copy strictly inside s exactly when some interior position ends a match of the length-k prefix. What is the largest such match length that ends anywhere from the second character up to the second-to-last one, computed by one linear pass?

Hint 3

Once you have that largest interior match value, walk the borders from longest to shortest and take the first one that does not exceed it. Careful: the longest border itself is not always valid, so the check must go all the way down the chain.

report an issue Prefer your own editor? sooks prep 76 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.