#236MediumString Matching· Prefix Function and Periods
Repeating Unit Finder
Input format
`s`: a non-empty string of lowercase letters.
Output format
A string: the shortest repeating unit.
Examples
Input: s = "abcabcabc"
Output: "abc"
Writing abc three times gives the signature, and no shorter unit does.
Input: s = "abaab"
Output: "abaab"
No proper block repeats to form abaab, so the unit is the whole string.
Input: s = "zzzz"
Output: "z"
The single letter z repeated four times is the signature.
Constraints
- 1 <= s.length <= 10^5
- `s` contains only lowercase English letters.
- #strings
- #kmp
- #prefix-function
- #periodicity