A monitoring dashboard encodes a sensor's recent readings as a string of letters and wants to know how often the pattern repeats, even if the last cycle is cut short.
A period of signal is a number p with 1 <= p <= signal.length such that signal[i] === signal[i + p] for every i where both exist. Write shortestPeriod(signal) that returns the smallest period.
For example abcabca has period 3: it is abc repeated, with the last copy cut short.
Signals can be 1,000,000 letters long, so checking every shift from scratch is too slow.
Input: signal = "abcabca" Output: 3
Every letter equals the one 3 places later.
Input: signal = "abcd" Output: 4
Nothing repeats, so only the full length works.
1 <= signal.length <= 10^6signal contains lowercase letters.Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #620 Shortest Period of a Signal. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.