Back to problems

Most Frequent Substring

Algorithm · Hubspot · Medium

You are building a text analysis tool that needs to find the most common substring of a fixed length within a lowercase string. Write a function that, given a string str and an integer n, returns the length-n substring that occurs most frequently. If there is a tie for the highest frequency, return the lexicographically smallest among the tied substrings. If no valid substring exists (e.g., n is larger than the string length, though constraints ensure it's not), return an…

Checking your access…