给定一个字符串S和整数M 。任务是精确执行M 次操作以获得字典序最小的字符串 。
- 在每个操作中,从字符串最优选择一个字符,并用紧邻的下一个字符( aaa -> aab ) 更新它,使字符串保持字典序最小。
- 允许对字符串的单个字符进行多次操作。
注意:将“z”的下一个视为“a”。
例子:
Input: S = “aazzx”, M = 6
Output: aaaab
Explanation:
We try to form lexicographical smallest string for each operations.
For m = 1: update “aazzx” to “aaazx”
for m = 2: update “aaazx” to “aaaax”
for m = 3: update “aaaax” to “aaaay”
for m = 4: update “aaaay” to “aaaaz”
for m = 5: update “aaaaz” to “aaaaa”
for m = 6: update “aaaaa” to “aaaab” which is lexicographical smallest than “aaaba”, “aabaa”, “abaaa”, “baaaa”.
Final string after 6 operations: “aaaab”.
Input: S = “z”, M = 27
Output: a
Explanation:
Try to form lexicographical smallest string for each operations, since there is only single character so all 27 operations have to performed over it. Final string after 27 operation is “a”.
方法:我们的想法是要实现一个简单的贪婪方法,迭代从字符串的开始字符串,并同时字符串的电流元素上进行迭代的重点,使之尽可能小。
- 假设,当前元素是r ,为了使r最小,即a ,需要 9 次操作,让这个值称为距离。
- 现在,检查 M 是否大于等于distance ,然后将当前字符更新为 ‘a’ 并将 M 的值减少distance 。否则继续下一次迭代。
- 由于z 的下一个是a ,形成了 26 的循环。所以字符串的最后一个字符,可以用最后一个字符+ (M % 26) 更新。
下面是上述方法的实现:
C++
// C++ implementation to find the
// lexicographical smallest string
// after performing M operations
#include
using namespace std;
// Function to find the
// lexicographical smallest string
// after performing M operations
void smallest_string(string s, int m)
{
// Size of the given string
int n = s.size();
// Declare an array a
int a[n];
// For each i, a[i] contain number
// of operations to update s[i] to 'a'
for (int i = 0; i < n; i++) {
int distance = s[i] - 'a';
if (distance == 0)
a[i] = 0;
else
a[i] = 26 - distance;
}
for (int i = 0; i < n; i++) {
// Check if m >= ar[i],
// then update s[i] to 'a'
// decrement k by a[i]
if (m >= a[i]) {
s[i] = 'a';
m = m - a[i];
}
}
// Form a cycle of 26
m = m % 26;
// update last element of
// string with the value
// s[i] + (k % 26)
s[n - 1] = s[n - 1] + m;
// Return teh answer
cout << s;
}
// Driver code
int main()
{
string str = "aazzx";
int m = 6;
smallest_string(str, m);
return 0;
}
Java
// Java implementation to find the
// lexicographical smallest String
// after performing M operations
class GFG{
// Function to find the
// lexicographical smallest String
// after performing M operations
static void smallest_String(char []s, int m)
{
// Size of the given String
int n = s.length;
// Declare an array a
int []a = new int[n];
// For each i, a[i] contain number
// of operations to update s[i] to 'a'
for (int i = 0; i < n; i++)
{
int distance = s[i] - 'a';
if (distance == 0)
a[i] = 0;
else
a[i] = 26 - distance;
}
for (int i = 0; i < n; i++)
{
// Check if m >= ar[i],
// then update s[i] to 'a'
// decrement k by a[i]
if (m >= a[i])
{
s[i] = 'a';
m = m - a[i];
}
}
// Form a cycle of 26
m = m % 26;
// update last element of
// String with the value
// s[i] + (k % 26)
s[n - 1] = (char) (s[n - 1] + m);
// Return teh answer
System.out.print(String.valueOf(s));
}
// Driver code
public static void main(String[] args)
{
String str = "aazzx";
int m = 6;
smallest_String(str.toCharArray(), m);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation to find the
# lexicographical smallest string
# after performing M operations
# Function to find the
# lexicographical smallest string
# after performing M operations
def smallest_string(s, m):
# Size of the given string
n = len(s);
l = list(s)
# Declare an array a
a = [0] * n;
# For each i, a[i] contain number
# of operations to update s[i] to 'a'
for i in range(n):
distance = ord(s[i]) - ord('a');
if (distance == 0):
a[i] = 0;
else:
a[i] = 26 - distance;
for i in range(n):
# Check if m >= ar[i],
# then update s[i] to 'a'
# decrement k by a[i]
if (m >= a[i]):
l[i] = 'a';
m = m - a[i];
# Form a cycle of 26
m = m % 26;
# update last element of
# with the value
# s[i] + (k % 26)
# Return teh answer
for i in range(len(l) - 1):
print(l[i], end = "")
print(chr(ord(l[n - 1]) + m))
# Driver code
str = "aazzx";
m = 6;
smallest_string(str, m);
# This code is contributed by grand_master
C#
// C# implementation to find the
// lexicographical smallest String
// after performing M operations
using System;
class GFG{
// Function to find the
// lexicographical smallest String
// after performing M operations
static void smallest_String(char []s, int m)
{
// Size of the given String
int n = s.Length;
// Declare an array a
int []a = new int[n];
// For each i, a[i] contain number
// of operations to update s[i] to 'a'
for(int i = 0; i < n; i++)
{
int distance = s[i] - 'a';
if (distance == 0)
a[i] = 0;
else
a[i] = 26 - distance;
}
for(int i = 0; i < n; i++)
{
// Check if m >= ar[i],
// then update s[i] to 'a'
// decrement k by a[i]
if (m >= a[i])
{
s[i] = 'a';
m = m - a[i];
}
}
// Form a cycle of 26
m = m % 26;
// Update last element of
// String with the value
// s[i] + (k % 26)
s[n - 1] = (char)(s[n - 1] + m);
// Return teh answer
Console.Write(String.Join("", s));
}
// Driver code
public static void Main(String[] args)
{
String str = "aazzx";
int m = 6;
smallest_String(str.ToCharArray(), m);
}
}
// This code is contributed by Princi Singh
aaaab
时间复杂度: O(N),其中 N 是给定字符串的长度
辅助空间: O(N),其中 N 是给定字符串的长度
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live