Open In App

Lexicographically smallest string formed repeatedly deleting character from substring 10

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:



  1. Removing the  S[1](=1)  from the substring, “10” over the range [1, 2] modifies the string S as S = “1001101”.
  2. Removing the  S[0](=1)  from the substring, “10” over the range [0, 1] modifies the string S as S = “001101”.
  3. Removing the  S[3](=1)  from the substring, “10” over the range [3, 4] modifies the string S as S = “00101”.
  4. Removing the  S[2](=1)  from the substring, “10” over the range [2, 3] modifies the string S as S = “0001”.
  5. 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: 

Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find smallest lexicogra-
// phically smallest string
string lexicographicallySmallestString(string S, int N)
{
    // Stores the index of last
    // occurring 0
    int LastZe = -1;
 
    // Stores the lexicographically
    // smallest string
    string ans;
 
    // Traverse the string S
    for (int i = N - 1; i >= 0; i--) {
 
        // If str[i] is 0
        if (S[i] == '0') {
 
            // Assign i to lastZe
            LastZe = i;
            break;
        }
    }
 
    // Traverse the string str
    for (int i = 0; i < N; i++) {
 
        // If i is less than or equal
        // to lastZe and str[i] is 0
        if (i <= LastZe && S[i] == '0')
            ans += S[i];
 
        // If i is greater than lastZe
        else if (i > LastZe)
            ans += S[i];
    }
 
    // Return ans
    return ans;
}
 
// Driver Code
int main()
{
    // Input
    string S = "11001101";
    int N = S.size();
 
    // Function Call
    cout << lexicographicallySmallestString(S, N);
 
    return 0;
}




// Java program for the above approach
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find smallest lexicogra-
// phically smallest string
static String lexicographicallySmallestString(String S,
                                              int N)
{
     
    // Stores the index of last
    // occurring 0
    int LastZe = -1;
 
    // Stores the lexicographically
    // smallest string
    String ans = "";
 
    // Traverse the string S
    for(int i = N - 1; i >= 0; i--)
    {
         
        // If str[i] is 0
        if (S.charAt(i) == '0')
        {
             
            // Assign i to lastZe
            LastZe = i;
            break;
        }
    }
 
    // Traverse the string str
    for(int i = 0; i < N; i++)
    {
         
        // If i is less than or equal
        // to lastZe and str[i] is 0
        if (i <= LastZe && S.charAt(i) == '0')
            ans += S.charAt(i);
 
        // If i is greater than lastZe
        else if (i > LastZe)
            ans += S.charAt(i);
    }
     
    // Return ans
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Input
    String S = "11001101";
    int N = S.length();
 
    // Function Call
    System.out.println(
        lexicographicallySmallestString(S, N));
}
}
 
// This code is contributed by avijitmondal1998




# Python program for the above approach
 
# Function to find smallest lexicogra-
# phically smallest string
def lexicographicallySmallestString(S, N):
   
    # Stores the index of last
    # occurring 0
    LastZe = -1
 
    # Stores the lexicographically
    # smallest string
    ans = ""
 
    # Traverse the S
    for i in range(N - 1, -1, -1):
       
        # If str[i] is 0
        if (S[i] == '0'):
 
            # Assign i to lastZe
            LastZe = i
            break
 
    # Traverse the str
    for  i in range(N):
        # If i is less than or equal
        # to lastZe and str[i] is 0
        if (i <= LastZe and S[i] == '0'):
            ans += S[i]
 
        # If i is greater than lastZe
        elif (i > LastZe):
            ans += S[i]
 
    # Return ans
    return ans
 
# Driver Code
if __name__ == '__main__':
    # Input
    S = "11001101"
    N = len(S)
 
    # Function Call
    print (lexicographicallySmallestString(S, N))
 
# This code is contributed by mohit kumar 29.




// C# program for the above approach
using System;
class GFG {
    // Function to find smallest lexicogra-
    // phically smallest string
    static string lexicographicallySmallestString(string S,
                                                  int N)
    {
        // Stores the index of last
        // occurring 0
        int LastZe = -1;
 
        // Stores the lexicographically
        // smallest string
        string ans = "";
 
        // Traverse the string S
        for (int i = N - 1; i >= 0; i--) {
 
            // If str[i] is 0
            if (S[i] == '0') {
 
                // Assign i to lastZe
                LastZe = i;
                break;
            }
        }
 
        // Traverse the string str
        for (int i = 0; i < N; i++) {
 
            // If i is less than or equal
            // to lastZe and str[i] is 0
            if (i <= LastZe && S[i] == '0')
                ans += S[i];
 
            // If i is greater than lastZe
            else if (i > LastZe)
                ans += S[i];
        }
 
        // Return ans
        return ans;
    }
 
    // Driver Code
    public static void Main()
    {
        // Input
        string S = "11001101";
        int N = S.Length;
 
        // Function Call
        Console.Write(
            lexicographicallySmallestString(S, N));
    }
}
 
// This code is contributed by ukasp.




<script>
 
// JavaScript program for the above approach
 
// Function to find smallest lexicogra-
// phically smallest string
function lexicographicallySmallestString(S, N)
{
     
    // Stores the index of last
    // occurring 0
    var LastZe = -1;
 
    // Stores the lexicographically
    // smallest string
    var ans = "";
 
    // Traverse the string S
    for(var i = N - 1; i >= 0; i--)
    {
         
        // If str[i] is 0
        if (S.charAt(i) == '0')
        {
             
            // Assign i to lastZe
            LastZe = i;
            break;
        }
    }
 
    // Traverse the string str
    for(var i = 0; i < N; i++)
    {
         
        // If i is less than or equal
        // to lastZe and str[i] is 0
        if (i <= LastZe && S.charAt(i) == '0')
            ans += S.charAt(i);
 
        // If i is greater than lastZe
        else if (i > LastZe)
            ans += S.charAt(i);
    }
     
    // Return ans
    return ans;
}
 
// Driver code
 
// Input
var S = "11001101";
var N = S.length;
 
// Function Call
document.write(lexicographicallySmallestString(S, N));
 
// This code is contributed by shivanisinghss2110
 
</script>

Output
0001

Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :