📌  相关文章
📜  从子字符串 10 中重复删除字符形成的字典序最小字符串

📅  最后修改于: 2021-09-24 04:59:05             🧑  作者: Mango

给定一个长度为N的二进制字符串S ,任务是通过选择任何子字符串“10”并从该子字符串中删除任何一个字符,任意次数地修改该字符串后,按字典顺序找到最小的字符串。

例子:

方法:根据以下观察可以解决给定的问题:

  • 可以观察到,最后一个零后面的字符’ 1 ‘不能被删除,因为一个人将无法找到任何“ 10 ”子串。
  • 按照字典顺序,最小的字符串在第一个字符串将包含尽可能多的零。
  • 可以观察到,如果每个“ 1 ”后面至少有一个“ 0 ”,则可以删除它。
  • 因此,我们的想法是从字符串删除最后一个出现的 ‘ 0 ‘ 之前的所有那些。

请按照以下步骤解决问题:

  • 初始化两个变量,比如ansLastZe,以存储结果字符串和最后出现的“ 0 ”的索引。
  • 使用变量i遍历字符串S的字符,然后如果S[i]是“ 0 ”,则将i分配给LastZe。
  • 使用变量i迭代字符串S的字符并执行以下操作:
    • 如果S[i] = ‘0’i ≤ LastZe,则将S[i]附加到ans。
    • 否则,如果i > LastZe,则将S[i]附加到ans
  • 最后,完成上述步骤后,将结果打印为ans

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find smallest lexicogra-
// phically smallest string
string lexicographicallySmallestString(string S, int N)
{
    // Stores the index of last
    // occuring 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
// 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
    // occuring 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


Python3
# Python program for the above approach
 
# Function to find smallest lexicogra-
# phically smallest string
def lexicographicallySmallestString(S, N):
   
    # Stores the index of last
    # occuring 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#
// 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
        // occuring 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.


Javascript


输出
0001

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程