Minimum rotations
Practice
3.6 (13 votes)
Algorithms
String manipulation
Two pointer
Problem
70% Success 1966 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a string \(s\) that contains lowercase alphabets. The length of this string is \(n\). While performing an operation, you can rotate the string by one character in the clockwise direction.

The string must satisfy the following condition:

You are given a character \(c\) and two integers \(f\) and \(k\). Determine the minimum number of operations that you must perform so that the prefix of length \(k\) has at least \(f\) occurrences of character \(c\).

Note: It may be impossible to make the string satisfy this condition.

Input format

  • First line: \(t\) denoting the number of test cases
  • For each test case (\(2 \times t\) lines):
    • First line: Space-separated integers \(n,\ f,\ k,\ c\) 
    • Second line: A string of size \(n\)

Output format

For each test case, print a single line denoting the required minimum number of rotations if it is possible to satisfy the condition. Otherwise, print \(-1\).

Constraints

\(1 \le t \le 10\\ 1 \le n \le 10^5\\ 1 \le f \le n\\ 1 \le k \le n\\ c is a lower case alphabet\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
42 votes
Tags:
Data StructuresEasySortingString Manipulationapproved
Points:20
7 votes
Tags:
AlgorithmsEasyString Manipulation
Points:20
3 votes
Tags:
String AlgorithmsReal worldAlgorithmsString Searching