Mid/SeniorGreedyString

Lexicographically Smallest String After Operations With Constraint

You are given a string s and an integer k.

Define a function distance(s1, s2) between two strings s1 and s2 of the same length n as:

  • The sum of the minimum distance between s1[i] and s2[i] when the characters from 'a' to 'z' are placed in a cyclic order, for all i in the range [0, n - 1].

For example, distance("ab", "cd") == 4, and distance("a", "z") == 1.

You can change any letter of s to any other lowercase English letter, any number of times.

Return a string denoting the lexicographically smallest string t you can get after some changes, such that distance(s, t) <= k.

Example 1
Inputs = "zbbz", k = 3
Output"aaaz"
Change s to "aaaz"; the distance between "zbbz" and "aaaz" is equal to k = 3.
Example 2
Inputs = "xaxcd", k = 4
Output"aawcd"
The distance between "xaxcd" and "aawcd" is equal to k = 4.

Constraints

  • 1 <= s.length <= 100
  • 0 <= k <= 2000
  • s consists only of lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate