Trains the technique from
LeetCode 1915Number of Wonderful SubstringsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A probe reports ten kinds of event, written as the lowercase letters a through j. One session of reports arrives as the string stream, one character per event, in the order they happened.
A stretch is any non-empty contiguous run of the session. A stretch is nearly even when at most one of the ten kinds occurs an odd number of times inside it, so either every kind present occurs an even number of times, or exactly one kind occurs an odd number of times and all the others occur an even number of times.
Return how many nearly even stretches the session contains. Two stretches that cover different positions count separately even when they read the same.
Example 1
Four stretches are nearly even: each of the three single events, where the one kind present occurs once, and the whole session `efe`, where `e` occurs twice and `f` once.
Example 2
Seven stretches are nearly even: the four single events, the stretch `gg` at indices 0 and 1 where `g` occurs twice, and the stretches `ggh` and `ghg`, each holding `g` twice and `h` once.
Example 3
Eight stretches are nearly even, among them the whole session, where `h` occurs twice and `i` occurs twice, so no kind at all occurs an odd number of times.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_nearly_even(stream: str) -> int:public long countNearlyEven(String stream)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.