Lexicographically smallest string formed repeatedly deleting character from substring 10
Last Updated :
24 Apr, 2023
Given a binary string S of length N, the task is to find lexicographically the smallest string formed after modifying the string by selecting any substring “10” and removing any one of the characters from that substring, any number of times.
Examples:
Input: S = “0101”
Output: 001
Explanation:
Removing the S[1](=1) from the substring, “10” over the range [1, 2] modifies the string S to “001”, which is the smallest.
Input: S =”11001101″
Output: 0001
Explanation:
One possible way to obtain Lexicographically the smallest string is:
- Removing the S[1](=1) from the substring, “10” over the range [1, 2] modifies the string S as S = “1001101”.
- Removing the S[0](=1) from the substring, “10” over the range [0, 1] modifies the string S as S = “001101”.
- Removing the S[3](=1) from the substring, “10” over the range [3, 4] modifies the string S as S = “00101”.
- Removing the S[2](=1) from the substring, “10” over the range [2, 3] modifies the string S as S = “0001”.
- Now any character can not be removed.
Therefore, the lexicographically smallest obtained string is “0001”.
Approach: The given problem can be solved based on the following observations:
- It can be observed that the character ‘1‘ after the last zero can not be removed as one will not be able to find any “10” substring.
- Lexicographically, the smallest string will contain as much as zero as it can before the first one.
- It can be observed that every ‘1‘ can be deleted if there is at least one ‘0‘ after it.
- Therefore, the idea is to remove all the ones before the last occurring ‘0‘ from the string.
Follow the steps below to solve the problem:
- Initialize two variables, say ans and LastZe, to store the resulting string and the index of last occurring ‘0‘.
- Iterate over the characters of string S using the variable i and then if S[i] is ‘0‘ then assign i to LastZe.
- Iterate over the characters of string S using the variable i and perform the following operations:
- If S[i] = ‘0’ and i ? LastZe, then append S[i] to ans.
- Otherwise, if i > LastZe, then append S[i] to ans.
- Finally, after completing the above steps, print the result as ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string lexicographicallySmallestString(string S, int N)
{
int LastZe = -1;
string ans;
for ( int i = N - 1; i >= 0; i--) {
if (S[i] == '0' ) {
LastZe = i;
break ;
}
}
for ( int i = 0; i < N; i++) {
if (i <= LastZe && S[i] == '0' )
ans += S[i];
else if (i > LastZe)
ans += S[i];
}
return ans;
}
int main()
{
string S = "11001101" ;
int N = S.size();
cout << lexicographicallySmallestString(S, N);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static String lexicographicallySmallestString(String S,
int N)
{
int LastZe = - 1 ;
String ans = "" ;
for ( int i = N - 1 ; i >= 0 ; i--)
{
if (S.charAt(i) == '0' )
{
LastZe = i;
break ;
}
}
for ( int i = 0 ; i < N; i++)
{
if (i <= LastZe && S.charAt(i) == '0' )
ans += S.charAt(i);
else if (i > LastZe)
ans += S.charAt(i);
}
return ans;
}
public static void main(String[] args)
{
String S = "11001101" ;
int N = S.length();
System.out.println(
lexicographicallySmallestString(S, N));
}
}
|
Python3
def lexicographicallySmallestString(S, N):
LastZe = - 1
ans = ""
for i in range (N - 1 , - 1 , - 1 ):
if (S[i] = = '0' ):
LastZe = i
break
for i in range (N):
if (i < = LastZe and S[i] = = '0' ):
ans + = S[i]
elif (i > LastZe):
ans + = S[i]
return ans
if __name__ = = '__main__' :
S = "11001101"
N = len (S)
print (lexicographicallySmallestString(S, N))
|
C#
using System;
class GFG {
static string lexicographicallySmallestString( string S,
int N)
{
int LastZe = -1;
string ans = "" ;
for ( int i = N - 1; i >= 0; i--) {
if (S[i] == '0' ) {
LastZe = i;
break ;
}
}
for ( int i = 0; i < N; i++) {
if (i <= LastZe && S[i] == '0' )
ans += S[i];
else if (i > LastZe)
ans += S[i];
}
return ans;
}
public static void Main()
{
string S = "11001101" ;
int N = S.Length;
Console.Write(
lexicographicallySmallestString(S, N));
}
}
|
Javascript
<script>
function lexicographicallySmallestString(S, N)
{
var LastZe = -1;
var ans = "" ;
for ( var i = N - 1; i >= 0; i--)
{
if (S.charAt(i) == '0' )
{
LastZe = i;
break ;
}
}
for ( var i = 0; i < N; i++)
{
if (i <= LastZe && S.charAt(i) == '0' )
ans += S.charAt(i);
else if (i > LastZe)
ans += S.charAt(i);
}
return ans;
}
var S = "11001101" ;
var N = S.length;
document.write(lexicographicallySmallestString(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...