Code RoomDistinct k-length substrings
MediumPrep Room Coding #638

Distinct k-length substrings

CodingAlgorithms & data structuresMid–Senior~20 min

Given a string s and an integer k, return the number of distinct substrings of s that have exactly length k. If k is greater than len(s) or k <= 0, return 0. For example, in "banana" the length-2 substrings are "ba", "an", "na", "an", "na", of which 3 are distinct. The string can be long, so the intended solution slides a fixed-size window (a rolling hash being one classic way to do it).

Implement
distinct_substrings_of_len_k(s: str, k: int) → int
Examples
in["banana",2]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 20 min
InputExpectedGot
["banana",2]3not run yetsample