Skip to content

2516. Take K of Each Character From Left and Right #855

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can use a sliding window technique with two pointers to find the minimum number of minutes needed to take at least k of each character ('a', 'b', 'c') from both the left and right of the string.

Problem Breakdown:

  • We are given a string s containing only 'a', 'b', and 'c'.
  • We need to take at least k occurrences of each character, either from the leftmost or rightmost characters of the string.
  • We need to determine the minimum number of minutes required to achieve this or return -1 if it's impossible.

Approach:

  1. Initial Checks:

    • If k == 0, we can directly return 0 since no characters are required.
    • If k exceeds the number of occurrences of any character in the string, return -1 immediat…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Nov 20, 2024
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants