Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

LeetCode Strings or Arrays

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is LeetCode Strings or Arrays. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Microsoft Teams mobile can group message text into compact previews. Given a message string s and an integer k, return the length of the longest contiguous substring containing at most k distinct characters.

Characters are case-sensitive, and spaces and punctuation count as characters. Return 0 when k is 0 or the string is empty.

Formal Specification

  • Input: A string s and an integer k.
  • Output: An integer representing the maximum length of a contiguous substring of s with at most k distinct characters.

Constraints

  • 0 <= k <= 50
  • 0 <= len(s) <= 10^5
  • s contains printable ASCII characters
  • Characters are case-sensitive
  • Spaces and punctuation count as distinct characters

Function Signature

def longest_k_distinct(s, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output