-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3106-LexicographicallySmallestStringAfterOperationsWithConstraint.go
More file actions
73 lines (63 loc) · 2.28 KB
/
3106-LexicographicallySmallestStringAfterOperationsWithConstraint.go
File metadata and controls
73 lines (63 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
// 3106. 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:
// Input: s = "zbbz", k = 3
// Output: "aaaz"
// Explanation:
// Change s to "aaaz". The distance between "zbbz" and "aaaz" is equal to k = 3.
// Example 2:
// Input: s = "xaxcd", k = 4
// Output: "aawcd"
// Explanation:
// The distance between "xaxcd" and "aawcd" is equal to k = 4.
// Example 3:
// Input: s = "lol", k = 0
// Output: "lol"
// Explanation:
// It's impossible to change any character as k = 0.
// Constraints:
// 1 <= s.length <= 100
// 0 <= k <= 2000
// s consists only of lowercase English letters.
import "fmt"
func getSmallestString(s string, k int) string {
bytes := []byte(s)
min := func (x, y int) int { if x < y { return x; }; return y; }
for i := 0; i < len(bytes) && k > 0; i++ {
distA := min(int(bytes[i]-'a'), int('z'-bytes[i]+1))
if distA <= k {
bytes[i] = 'a'
} else {
bytes[i] -= byte(k)
}
k -= distA
}
return string(bytes)
}
func main() {
// Example 1:
// Input: s = "zbbz", k = 3
// Output: "aaaz"
// Explanation:
// Change s to "aaaz". The distance between "zbbz" and "aaaz" is equal to k = 3.
fmt.Println(getSmallestString("zbbz", 3)) // "aaaz"
// Example 2:
// Input: s = "xaxcd", k = 4
// Output: "aawcd"
// Explanation:
// The distance between "xaxcd" and "aawcd" is equal to k = 4.
fmt.Println(getSmallestString("xaxcd", 4)) // "aawcd"
// Example 3:
// Input: s = "lol", k = 0
// Output: "lol"
// Explanation:
// It's impossible to change any character as k = 0.
fmt.Println(getSmallestString("lol", 0)) // "lol"
}