Lexicographically largest string possible by reversing substrings having even number of 1s
Last Updated :
31 May, 2021
Given a binary string S, the task is to convert the given string S to its lexicographic maximum form by reversing substrings having an even number of 1s.
Examples:
Input: S = “10101”
Output: 11010
Explanation:
Reversing the substring {S[0], …, S[2]} modifies S to “10101”.
Reversing the substring {S[1], …, S[4]} modifies S to “11010”.
Now, any other substring cannot be chosen as each will either have odd number of 1s or will be lexicographically smaller than the previous string. Thus, 11010 is the lexicographically largest string that can be performed.
Input: S = “0101”
Output: 1010
Approach: The idea is to start from the first index and traverse the string till the total number of 1s in the string becomes even. As soon as an index is found where there is exactly an even number of 1s, reverse the string from the starting index to the end index. Repeat this process for every index to get the lexicographically largest string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void lexicographicallyMax(string s)
{
int n = s.size();
for ( int i = 0; i < n; i++) {
int count = 0;
int beg = i;
int end = i;
if (s[i] == '1' )
count++;
for ( int j = i + 1; j < n; j++) {
if (s[j] == '1' )
count++;
if (count % 2 == 0
&& count != 0) {
end = j;
break ;
}
}
reverse(s.begin() + beg,
s.begin() + end + 1);
}
cout << s << "\n" ;
}
int main()
{
string S = "0101" ;
lexicographicallyMax(S);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void lexicographicallyMax(String s)
{
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
int count = 0 ;
int beg = i;
int end = i;
if (s.charAt(i) == '1' )
count++;
for ( int j = i + 1 ; j < n; j++)
{
if (s.charAt(j) == '1' )
count++;
if (count % 2 == 0 &&
count != 0 )
{
end = j;
break ;
}
}
s = reverse(s, beg, end + 1 );
}
System.out.println(s);
}
static String reverse(String s, int beg, int end)
{
StringBuilder x = new StringBuilder( "" );
for ( int i = 0 ; i < beg; i++)
x.append(s.charAt(i));
for ( int i = end - 1 ; i >= beg; i--)
x.append(s.charAt(i));
for ( int i = end; i < s.length(); i++)
x.append(s.charAt(i));
return x.toString();
}
public static void main(String args[])
{
String S = "0101" ;
lexicographicallyMax(S);
}
}
|
Python3
def lexicographicallyMax(s):
n = len (s)
for i in range (n):
count = 0
beg = i
end = i
if (s[i] = = '1' ):
count + = 1
for j in range (i + 1 , n):
if (s[j] = = '1' ):
count + = 1
if (count % 2 = = 0 and count ! = 0 ):
end = j
break
temp = s[beg : end + 1 ]
temp = temp[:: - 1 ]
s = s[ 0 : beg] + temp + s[end + 1 :]
print (s)
S = "0101"
lexicographicallyMax(S)
|
C#
using System;
using System.Text;
class GFG
{
static void lexicographicallyMax(String s)
{
int n = s.Length;
for ( int i = 0; i < n; i++)
{
int count = 0;
int beg = i;
int end = i;
if (s[i] == '1' )
count++;
for ( int j = i + 1; j < n; j++)
{
if (s[j] == '1' )
count++;
if (count % 2 == 0 &&
count != 0)
{
end = j;
break ;
}
}
s = reverse(s, beg, end + 1);
}
Console.WriteLine(s);
}
static String reverse(String s, int beg, int end)
{
StringBuilder x = new StringBuilder( "" );
for ( int i = 0; i < beg; i++)
x.Append(s[i]);
for ( int i = end - 1; i >= beg; i--)
x.Append(s[i]);
for ( int i = end; i < s.Length; i++)
x.Append(s[i]);
return x.ToString();
}
public static void Main(String []args)
{
String S = "0101" ;
lexicographicallyMax(S);
}
}
|
Javascript
<script>
function lexicographicallyMax(s)
{
var n = s.length;
for ( var i = 0; i < n; i++) {
var count = 0;
var beg = i;
var end = i;
if (s[i] == '1' )
count++;
for ( var j = i + 1; j < n; j++) {
if (s[j] == '1' )
count++;
if (count % 2 == 0
&& count != 0) {
end = j;
break ;
}
}
for ( var i = beg; i<parseInt((end+1)/2);i++)
{
let temp = s[i] ;
s[i] = s[end - i+1] ;
s[end -i+1 ] = temp;
}
}
document.write( s.join( "" ) + "<br>" );
}
var S = "0101" .split( '' );
lexicographicallyMax(S);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...