Tap here to switch tabs
Problem
Submissions

Problem: Take K of Each Character From Left and Right

med
30 min
Try to solve the Take K of Each Character From Left and Right problem.

Statement

You are given a string s consisting of the characters 'a', 'b', and 'c', and an integer k.

In each minute, you can choose to take either the leftmost character or the rightmost character of s.

Return the minimum number of minutes needed to take at least k of each character 'a', 'b', and 'c', or return −1-1 if it is not possible to obtain at least k of each character.

Constraints:

  • 1≤1 \leq s.length ≤105\leq 10^5

  • s consists of only the letters 'a', 'b', and 'c'.

  • 0≤0 \leq k ≤\leq s.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Take K of Each Character From Left and Right

med
30 min
Try to solve the Take K of Each Character From Left and Right problem.

Statement

You are given a string s consisting of the characters 'a', 'b', and 'c', and an integer k.

In each minute, you can choose to take either the leftmost character or the rightmost character of s.

Return the minimum number of minutes needed to take at least k of each character 'a', 'b', and 'c', or return −1-1 if it is not possible to obtain at least k of each character.

Constraints:

  • 1≤1 \leq s.length ≤105\leq 10^5

  • s consists of only the letters 'a', 'b', and 'c'.

  • 0≤0 \leq k ≤\leq s.length