AmazonOA #3
Self-Sufficient Substring
Estimated HardJudge-ready
Topics (AI-labelled)HashingTwo Pointers
Find the longest non-empty contiguous substring that is not the whole string such that, for every character appearing in it, all occurrences of that character in the original string lie inside the substring. If none exists, output 0.
Input
One line containing a string of lowercase English letters, of length 1..100000.
Output
Output a single integer, the answer required by the problem.
Example 1
Input:
abadgdg
Output:
4
Example 2
Input:
aaaaaaa
Output:
0
Example 3
Input:
abac
Output:
3
Example notes
Example 1: the substring dgdg contains every d and g of the original string and has length 4, and there is no longer self-contained proper substring. Example 2: any self-contained substring that contains a must contain all 7 a's, which conflicts with being a proper substring, so the output is 0. Example 3: aba contains every a and b, so the length is 3.
Editorial
Loading solution…