给定一个大小为N的字符串S和一个正整数K ,任务是对字符串S执行至多K 次操作,使其按字典顺序尽可能地最小。在一个操作中,交换S[i]和S[j] ,然后将S[i]更改为任何字符,给定1 ≤ i < j ≤ N 。
例子:
Input: S = “geek”, K = 5
Output: aaaa
Explanation:
In 1st operation: take i = 1 and j = 4, swap S[1] and S[4] and then change S[1] to ‘a’. Modified string = “aeeg”.
In 2nd operation: take i = 2 and j=4, swap S[2] and S[4] and then change S[2] to ‘a’. Modified string = “aaee”.
In 3rd operation: take i = 3 and j = 4, swap S[3] and S[4] and then change S[3] to ‘a’. Modified string = “aaae”.
In 4th operation: take i = 3 and j = 4, swap S[3] and S[4] and then change S[3] to ‘a’. Modified string = “aaaa”.
Input: S = “geeksforgeeks”, K = 6
Output: aaaaaaeegeeks
Explanation: After 6 operations, lexicographically smallest string will be “aaaaaaeegeeks”.
方法:对于K≥N ,字典序最小的可能字符串将是‘a’重复N次,因为在N 次操作中, S 的所有字符都可以更改为‘a’ 。对于所有其他情况,想法是使用变量i迭代字符串S ,找到一个合适的j ,其中S[j]>S[i] ,然后将S[j]转换为S[i]和S[i]到‘a’ 。当K>0 时继续这个过程。
请按照以下步骤解决问题:
- 如果K ≥ N ,将字符串S 的每个字符转换为‘a’并打印字符串S 。
- 除此以外:
- 使用变量i在范围[0, N-1] 中迭代
- 如果K=0 ,则跳出循环。
- 使用变量j在范围[i+1, N-1] 中迭代
- 如果S[j]>S[i] ,设置S[j]=S[i]并跳出循环。
- 如果没有找到这样的元素,即j=N设置S[j-1]=S[i] 。
- 设置S[i]=’a’并将K减 1。
- 打印字符串S 。
- 使用变量i在范围[0, N-1] 中迭代
下面是上述方法的实现:
C++
// C++ program to implement the above approach
#include
using namespace std;
// Function to find the lexicographically
// smallest possible string by performing
// K operations on string S
void smallestlexicographicstring(string s, int k)
{
// Store the size of string, s
int n = s.size();
// Check if k>=n, if true, convert
// every character to 'a'
if (k >= n) {
for (int i = 0; i < n; i++) {
s[i] = 'a';
}
cout << s;
return;
}
// Iterate in range[0, n - 1] using i
for (int i = 0; i < n; i++) {
// When k reaches 0, break the loop
if (k == 0) {
break;
}
// If current character is 'a',
// continue
if (s[i] == 'a')
continue;
// Otherwise, iterate in the
// range [i + 1, n - 1] using j
for (int j = i + 1; j < n; j++) {
// Check if s[j] > s[i]
if (s[j] > s[i]) {
// If true, set s[j] = s[i]
// and break out of the loop
s[j] = s[i];
break;
}
// Check if j reaches the last index
else if (j == n - 1)
s[j] = s[i];
}
// Update S[i]
s[i] = 'a';
// Decrement k by 1
k--;
}
// Print string
cout << s;
}
// Driver Code
int main()
{
// Given String, s
string s = "geeksforgeeks";
// Given k
int k = 6;
// Function Call
smallestlexicographicstring(s, k);
return 0;
}
Java
// Java program to implement the above approach
public class GFG
{
// Function to find the lexicographically
// smallest possible string by performing
// K operations on string S
static void smallestlexicographicstring(char[] s, int k)
{
// Store the size of string, s
int n = s.length;
// Check if k>=n, if true, convert
// every character to 'a'
if (k >= n)
{
for (int i = 0; i < n; i++)
{
s[i] = 'a';
}
System.out.print(s);
return;
}
// Iterate in range[0, n - 1] using i
for (int i = 0; i < n; i++)
{
// When k reaches 0, break the loop
if (k == 0)
{
break;
}
// If current character is 'a',
// continue
if (s[i] == 'a')
continue;
// Otherwise, iterate in the
// range [i + 1, n - 1] using j
for (int j = i + 1; j < n; j++)
{
// Check if s[j] > s[i]
if (s[j] > s[i])
{
// If true, set s[j] = s[i]
// and break out of the loop
s[j] = s[i];
break;
}
// Check if j reaches the last index
else if (j == n - 1)
s[j] = s[i];
}
// Update S[i]
s[i] = 'a';
// Decrement k by 1
k--;
}
// Print string
System.out.print(s);
}
// Driver code
public static void main(String[] args)
{
// Given String, s
char[] s = ("geeksforgeeks").toCharArray();
// Given k
int k = 6;
// Function Call
smallestlexicographicstring(s, k);
}
}
// This code is contributed by divyesh072019.
Python3
# Python3 program to implement the above approach
# Function to find the lexicographically
# smallest possible string by performing
# K operations on string S
def smallestlexicographicstring(s, k):
# Store the size of string, s
n = len(s)
# Check if k>=n, if true, convert
# every character to 'a'
if (k >= n):
for i in range(n):
s[i] = 'a';
print(s, end = '')
return;
# Iterate in range[0, n - 1] using i
for i in range(n):
# When k reaches 0, break the loop
if (k == 0):
break;
# If current character is 'a',
# continue
if (s[i] == 'a'):
continue;
# Otherwise, iterate in the
# range [i + 1, n - 1] using j
for j in range(i + 1, n):
# Check if s[j] > s[i]
if (s[j] > s[i]):
# If true, set s[j] = s[i]
# and break out of the loop
s[j] = s[i];
break;
# Check if j reaches the last index
elif (j == n - 1):
s[j] = s[i];
# Update S[i]
s[i] = 'a';
# Decrement k by 1
k -= 1
# Print string
print(''.join(s), end = '');
# Driver Code
if __name__=='__main__':
# Given String, s
s = list("geeksforgeeks");
# Given k
k = 6;
# Function Call
smallestlexicographicstring(s, k);
# This code is contributed by rutvik_56.
C#
// C# program to implement the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the lexicographically
// smallest possible string by performing
// K operations on string S
static void smallestlexicographicstring(char[] s, int k)
{
// Store the size of string, s
int n = s.Length;
// Check if k>=n, if true, convert
// every character to 'a'
if (k >= n)
{
for (int i = 0; i < n; i++)
{
s[i] = 'a';
}
Console.Write(s);
return;
}
// Iterate in range[0, n - 1] using i
for (int i = 0; i < n; i++)
{
// When k reaches 0, break the loop
if (k == 0)
{
break;
}
// If current character is 'a',
// continue
if (s[i] == 'a')
continue;
// Otherwise, iterate in the
// range [i + 1, n - 1] using j
for (int j = i + 1; j < n; j++)
{
// Check if s[j] > s[i]
if (s[j] > s[i])
{
// If true, set s[j] = s[i]
// and break out of the loop
s[j] = s[i];
break;
}
// Check if j reaches the last index
else if (j == n - 1)
s[j] = s[i];
}
// Update S[i]
s[i] = 'a';
// Decrement k by 1
k--;
}
// Print string
Console.Write(s);
}
// Driver code
static void Main()
{
// Given String, s
char[] s = ("geeksforgeeks").ToCharArray();
// Given k
int k = 6;
// Function Call
smallestlexicographicstring(s, k);
}
}
// This code is contributed by divyeshrabadiya07.
Javascript
aaaaaaeegeeks
时间复杂度: O(N 2 )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live