修改给定的字符串,使奇数和偶数索引在字典上是最大和最小的
给定一个由N个小写字母组成的字符串S ,任务是通过将所有字符替换为当前字符以外的字符来修改给定字符串,使得由奇数和偶数索引形成的后缀字符串在所有可能的字典中分别是最大和最小的字符串S的修改。
例子:
Input: S = “giad”
Output: azbz
Explanation:
Modify the given string S to “azbz”.
Now the suffixes starting at odd indices {zbz, z} are lexicographically largest among all possible replacements of characters.
And all the suffixes starting at even indices {azbz, bz} are lexicographically smallest among all possible replacements of characters.
Input: S = “ewdwnk”
Output: azazaz
方法:给定的问题可以通过使用贪心方法来解决。这个想法是用字符'z'替换所有奇数索引字符,如果存在字符'z'则用'y'替换它。同样,将所有偶数索引字符替换为字符'a' ,如果存在字符'a' ,则将其替换为'b' 。经过上述修改后,将字符串S打印为形成的结果字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to modify the given string
// satisfying the given criteria
string performOperation(string S, int N)
{
// Traverse the string S
for (int i = 0; i < N; i++) {
// If i is even
if (i % 2 == 0) {
// If the S[i] is 'a', then
// change S[i] to 'b'
if (S[i] == 'a') {
S[i] = 'b';
}
// Otherwise, change S[i]
// to 'a'
else {
S[i] = 'a';
}
}
else {
// If S[i] is 'z', then
// change S[i] to 'y'
if (S[i] == 'z') {
S[i] = 'y';
}
// Otherwise, change S[i]
// to 'z'
else {
S[i] = 'z';
}
}
}
// Return the result
return S;
}
// Driver Code
int main()
{
string S = "giad";
int N = S.size();
cout << performOperation(S, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to modify the given String
// satisfying the given criteria
static String performOperation(char[] S, int N)
{
// Traverse the String S
for (int i = 0; i < N; i++) {
// If i is even
if (i % 2 == 0) {
// If the S[i] is 'a', then
// change S[i] to 'b'
if (S[i] == 'a') {
S[i] = 'b';
}
// Otherwise, change S[i]
// to 'a'
else {
S[i] = 'a';
}
}
else {
// If S[i] is 'z', then
// change S[i] to 'y'
if (S[i] == 'z') {
S[i] = 'y';
}
// Otherwise, change S[i]
// to 'z'
else {
S[i] = 'z';
}
}
}
// Return the result
return String.valueOf(S);
}
// Driver Code
public static void main(String[] args)
{
String S = "giad";
int N = S.length();
System.out.print(performOperation(S.toCharArray(), N));
}
}
// This code is contributed by shikhasingrajput
Python3
# python program for the above approach
# Function to modify the given string
# satisfying the given criteria
def performOperation(S, N):
# Traverse the string S
# we cannot directly change string
# because it is immutable
# so change of list of char
S = list(S)
for i in range(0, N):
# If i is even
if (i % 2 == 0):
# If the S[i] is 'a', then
# change S[i] to 'b'
if (S[i] == 'a'):
S[i] = 'b'
# Otherwise, change S[i]
# to 'a'
else:
S[i] = 'a'
else:
# If S[i] is 'z', then
# change S[i] to 'y'
if (S[i] == 'z'):
S[i] = 'y'
# Otherwise, change S[i]
# to 'z'
else:
S[i] = 'z'
# Return the result
# join the list of char
return "".join(S)
# Driver Code
if __name__ == "__main__":
S = "giad"
N = len(S)
print(performOperation(S, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to modify the given string
// satisfying the given criteria
static string performOperation(string S, int N)
{
// Traverse the string S
for (int i = 0; i < N; i++) {
// If i is even
if (i % 2 == 0) {
// If the S[i] is 'a', then
// change S[i] to 'b'
if (S[i] == 'a') {
S = S.Substring(0, i) + 'b' + S.Substring(i + 1);
}
// Otherwise, change S[i]
// to 'a'
else {
S = S.Substring(0, i) + 'a' + S.Substring(i + 1);
}
}
else {
// If S[i] is 'z', then
// change S[i] to 'y'
if (S[i] == 'z') {
S = S.Substring(0, i) + 'y' + S.Substring(i + 1);
}
// Otherwise, change S[i]
// to 'z'
else {
S = S.Substring(0, i) + 'z' + S.Substring(i + 1);
}
}
}
// Return the result
return S;
}
// Driver Code
public static void Main()
{
string S = "giad";
int N = S.Length;
Console.Write(performOperation(S, N));
}
}
// This code is contributed by ipg2016107.
Javascript
azbz
时间复杂度: O(N)
辅助空间: O(1)