The smallest string
Practice
3.2 (33 votes)
Algorithms
Basics of greedy algorithms
Greedy algorithms
Problem
94% Success 5594 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given a string \(S\) which consists of lower case Latin letters and you need to perform the following operation exactly \(K\) times:
- Select any character and replace it with its next character ['a' with 'b', 'b' with 'c'.... 'z' with 'a'].
You need to find the lexicographically smallest string after performing exactly \(K\) operations on string \(S\).
Input format
- The first line contains an integer \(T\) denoting the number of test cases.
- The first line of each test case contains two space-separated integers \(N\) and \(K\) denoting the length of string \(S\) and the number of operations to be performed respectively.
- The second line of each test case contains string \(S\) consisting of lower case Latin letters.
Output format
Print \(T\) lines. For each test case, print a single lexicographic string \(S\) after performing exactly \(K\) operations.
Constraints
\(1 \leq T \leq 20000\)
\(1 \leq N \leq 200000\)
\(1 \leq K \leq 1e9\)
String \(S\) consists of lower case Latin letters
Sum of \(N\) over all test cases will not exceed 200000
Submissions
Please login to view your submissions
Editorial