The tune with no repeated notes
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- strings
- loops
Problem
"Give me a passage where no note sounds twice," the choir director asks Julian. He has the whole piece written on a single line, and he is after the longest unbroken stretch that fits.
Write a function that takes notes, a string where each character is a note, an uppercase letter from A to G (it may be empty), and returns a whole number: the length of the longest unbroken stretch in which no character repeats. Unbroken means no notes are skipped.
With "ABCAD" it returns 4: "ABC" cannot grow, because the A is already in it, but "BCAD" fits whole.
The repeat can be far away: in "CDC" the longest stretch is 2, not 3.
A single note is already a stretch of length 1, and an empty string is 0.
Examples
The example
"ABCAD" → 4
Nothing repeats
"CDEFG" → 5
The repeat is not next to it
"CDC" → 2
The same note four times
"EEEE" → 1
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def fresh_stretch(notes):
passJavaScript
function freshStretch(notes) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.