Eliminaciones de «01» o «10» en una string binaria para liberarla de «01» o «10»

Dada una string binaria str , la tarea es encontrar el recuento de eliminación de la substring «01» o «10» de la string para que la string dada esté libre de estas substrings. Imprime el número mínimo de eliminaciones.
Ejemplos: 
 

Entrada: str = “11010” 
Salida:
La string resultante será “1”
Entrada: str = “1000101” 
Salida:
String resultante, str = “0” 
 

Enfoque: estamos eliminando «01» y «10» y la string binaria contiene solo los caracteres ‘0’ y ‘1’ . Por lo tanto, el número mínimo de eliminaciones será igual al mínimo de la cuenta de ‘0’ y ‘1’.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of deletions
// of sub-strings "01" or "10"
int substrDeletion(string str, int len)
{
 
    // To store the count of 0s and 1s
    int count0 = 0, count1 = 0;
 
    for (int i = 0; i < len; i++) {
        if (str[i] == '0')
            count0++;
        else
            count1++;
    }
 
    return min(count0, count1);
}
 
// Driver code
int main()
{
    string str = "010";
    int len = str.length();
    cout << substrDeletion(str, len);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
     
// Function to return the count of deletions
// of sub-strings "01" or "10"
static int substrDeletion(String str, int len)
{
 
    // To store the count of 0s and 1s
    int count0 = 0, count1 = 0;
 
    for (int i = 0; i < len; i++)
    {
        if (str.charAt(i) == '0')
            count0++;
        else
            count1++;
    }
 
    return Math.min(count0, count1);
}
 
// Driver code
public static void main(String[] args)
{
    String str = "010";
    int len = str.length();
    System.out.println(substrDeletion(str, len));
}
}
 
// This code is contributed by Code_Mech.

Python3

# Python3 implementation of the approach
 
# Function to return the count of
# deletions of sub-strings "01" or "10"
def substrDeletion(string, length) :
 
    # To store the count of 0s and 1s
    count0 = 0;
    count1 = 0;
 
    for i in range(length) :
        if (string[i] == '0') :
            count0 += 1;
        else :
            count1 += 1;
 
    return min(count0, count1);
 
# Driver code
if __name__ == "__main__" :
 
    string = "010";
    length = len(string);
     
    print(substrDeletion(string, length));
 
# This code is contributed by Ryuga

C#

// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the count of deletions
// of sub-strings "01" or "10"
static int substrDeletion(string str, int len)
{
 
    // To store the count of 0s and 1s
    int count0 = 0, count1 = 0;
 
    for (int i = 0; i < len; i++)
    {
        if (str[i] == '0')
            count0++;
        else
            count1++;
    }
 
    return Math.Min(count0, count1);
}
 
// Driver code
public static void Main()
{
    string str = "010";
    int len = str.Length;
    Console.Write(substrDeletion(str, len));
}
}
 
// This code is contributed by Ita_c.

Javascript

<script>
// Javascript implementation of the approach
 
// Function to return the count of deletions
// of sub-strings "01" or "10"
function substrDeletion(str,len)
{
    // To store the count of 0s and 1s
    let count0 = 0, count1 = 0;
   
    for (let i = 0; i < len; i++)
    {
        if (str[i] == '0')
            count0++;
        else
            count1++;
    }
   
    return Math.min(count0, count1);
}
 
// Driver code
let str = "010";
let len = str.length;
document.write(substrDeletion(str, len));
 
 
         
// This code is contributed by rag2127
</script>
Producción: 

1

 

Publicación traducida automáticamente

Artículo escrito por SURENDRA_GANGWAR y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *