Skip to main content
LeetCode 2516, Medium. Topics: Hash Table, String, Sliding Window. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

Problem

You are given a string s consisting of the characters 'a', 'b', and 'c' and a non-negative integer k. Each minute, you may take either the leftmost character of s, or the rightmost character of s. Return the minimum number of minutes needed for you to take at least k of each character, or return -1 if it is not possible to take k of each character.

Examples

Constraints

  • 1 <= s.length <= 10^5
  • s consists of only the letters 'a', 'b', and 'c'.
  • 0 <= k <= s.length

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026