Modify given string such that odd and even indices is lexicographically largest and smallest
Given a string S consisting of N lowercase alphabets, the task is to modify the given string by replacing all the characters with characters other than the current character such that the suffix string formed from odd and even indices is lexicographically largest and smallest respectively among all possible modifications of the string S.
Examples:
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
Approach: The given problem can be solved by using the Greedy Approach. The idea is to replace all the odd indices characters with the character ‘z’ and if the character ‘z’ is present then replace it with ‘y’. Similarly, replace all the even indices characters with the character ‘a’ and if the character ‘a’ is present then replace it with ‘b’. After the above modifications, print the string S as the resultant string formed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string performOperation(string S, int N)
{
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (S[i] == 'a' ) {
S[i] = 'b' ;
}
else {
S[i] = 'a' ;
}
}
else {
if (S[i] == 'z' ) {
S[i] = 'y' ;
}
else {
S[i] = 'z' ;
}
}
}
return S;
}
int main()
{
string S = "giad" ;
int N = S.size();
cout << performOperation(S, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String performOperation( char [] S, int N)
{
for ( int i = 0 ; i < N; i++) {
if (i % 2 == 0 ) {
if (S[i] == 'a' ) {
S[i] = 'b' ;
}
else {
S[i] = 'a' ;
}
}
else {
if (S[i] == 'z' ) {
S[i] = 'y' ;
}
else {
S[i] = 'z' ;
}
}
}
return String.valueOf(S);
}
public static void main(String[] args)
{
String S = "giad" ;
int N = S.length();
System.out.print(performOperation(S.toCharArray(), N));
}
}
|
Python3
def performOperation(S, N):
S = list (S)
for i in range ( 0 , N):
if (i % 2 = = 0 ):
if (S[i] = = 'a' ):
S[i] = 'b'
else :
S[i] = 'a'
else :
if (S[i] = = 'z' ):
S[i] = 'y'
else :
S[i] = 'z'
return "".join(S)
if __name__ = = "__main__" :
S = "giad"
N = len (S)
print (performOperation(S, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static string performOperation( string S, int N)
{
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (S[i] == 'a' ) {
S = S.Substring(0, i) + 'b' + S.Substring(i + 1);
}
else {
S = S.Substring(0, i) + 'a' + S.Substring(i + 1);
}
}
else {
if (S[i] == 'z' ) {
S = S.Substring(0, i) + 'y' + S.Substring(i + 1);
}
else {
S = S.Substring(0, i) + 'z' + S.Substring(i + 1);
}
}
}
return S;
}
public static void Main()
{
string S = "giad" ;
int N = S.Length;
Console.Write(performOperation(S, N));
}
}
|
Javascript
<script>
function performOperation(S, N)
{
for ( var i = 0; i < N; i++) {
if (i % 2 == 0) {
if (S.charAt(i) == 'a' ) {
S[i] = 'b' ;
}
else {
S[i]= 'a' ;
}
}
else {
if (S.charAt(i) == 'z' ) {
S.charAt(i) = 'y' ;
}
else {
S[i] = 'z' ;
}
}
}
return S;
}
var S = "giad" ;
var N = S.length;
document.write(performOperation(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
29 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...