Document shingle count

Write codeEasy14 min

Your task

A help centre deduplicator measures how alike two articles are by cutting each one into shingles. A shingle is a run of exactly width consecutive characters, taken at every starting position, so neighbouring runs overlap in all but one character. A run holding a space is not a shingle, because the tool never lets one straddle a word break, and that run is dropped. Given document and width, return how many distinct shingles the document yields. A shingle that turns up in ten places still counts once, and shingles are compared exactly, so case matters. Return 0 when width is zero or negative, and 0 when width is longer than the document, since no run that size exists. This count is the denominator the tool divides shared shingles by.

Examples

unique_shingle_count(document: str, width: int) -> int
Example 1
Inputunique_shingle_count("banana", 3)
Output3
Example 2
Inputunique_shingle_count("hot pot", 3)
Output2
Example 3
Inputunique_shingle_count("aaaa", 2)
Output1