Eliminar elementos entre los dos ceros

Dado un número entero N que muestra el tamaño de la string y en la siguiente línea, una string que contiene una string de caracteres con solo cero y uno . La tarea es eliminar un solo carácter cada vez que se encuentre entre los dos caracteres cero.
Durante cada turno, solo se eliminará un carácter de la string que cumpla la siguiente condición: 
 

  • Debe estar rodeado de ceros en ambos lados.

Ejemplos: 
 

Input  : str = "1001
Output : str = "1001"

Input  : str = "10101
Output : str = "1001"

Use un bucle de 1 a N – 1 y verifique si algún elemento se encuentra entre dos ceros, de modo que s[i – 1] = ‘0’ y s[i + 1] = ‘0’. Si se cumple la condición, elimine el carácter en esa posición y comience a buscar patrones nuevamente.
 

C++

// C++ program to delete elements between zeros
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the string
// after operation
string findstring(string s)
{
    int n = s.length();
         
    // Traversing through string
    for (int i = 1; i < n - 1; i++)
    {
        // Checking for character
        // Between two zeros
        if ((s.at(i - 1) == '0' &&
            s.at(i + 1) == '0'))
        {
 
            // deleting the character
            // At specific position
            s.erase(i, 1);
             
            i--;
            if (i > 0 && s.at(i - 1) == '0')
                 i--;
                         
            // updating the length
            // of the string
            n = s.length();
        }
    }
             
    return s;
}
     
// Drivers code
int main() {
     
    cout << findstring("100100");
    return 0;
}

Java

// Java program to delete elements between zeros
import java.util.*;
 
public class GFG
{
    // Function to find the string
    // after operation
    static String findstring(String s)
    {
        int n = s.length();
             
        // use for loop to remove the
        // character between two zeros
        for (int i = 1; i < n - 1; i++)
        {
            // Checking for character
            // Between two zeros
            if ((s.charAt(i - 1) == '0' &&
                 s.charAt(i + 1) == '0'))
            {
                     
                // deleting the character
                // At specific position
                s = s.substring(0, i) + s.substring(i + 1);
                 
                i--;
                if (i > 0 && s.charAt(i - 1) == '0')
                    i--;
                         
                // updating the length
                // of the string
                n = s.length();
            }
        }
             
        return s;
    }
     
    // Driver code
    public static void main(String[] args)
    {
       String s="100100";
       System.out.println(findstring(s));
    }
}

Python3

# Python3 program to delete elements
# between zeros
 
# Function to find the string
# after operation
def findstring(s):
     
    n = len(s)
    s = list(s)
    i = 1
 
    # Traversing through string
    while i < n - 1:
 
        # Checking for character
        # Between two zeros
        if (s[i - 1] == '0' and
            s[i + 1] == '0'):
 
            # Deleting the character
            # At specific position
            s.pop(i)
 
            i -= 1
            if i > 0 and s[i - 1] == '0':
                i -= 1
 
            # Updating the length
            # of the string
            n = len(s)
        i += 1
 
    return ''.join(s)
 
# Driver code
if __name__ == '__main__':
 
    print (findstring('100100'))
 
# This code is contributed by rutvik_56

C#

// C# program to delete
// elements between zeros
using System;
 
class GFG
{
    // Function to find the
    // string after operation
    static string findstring(string s)
    {
        int n = s.Length;
        string st = "";
         
        // Traversing through string
        for (int i = 1; i < n - 1; i++)
        {
            // Checking for character
            // Between two zeros
            if ((s[i - 1] == '0' &&
                 s[i + 1] == '0'))
            {
     
                // deleting the character
                // At specific position
                st = s.Remove(i, 1);
                s = st;
                 
                i--;
                if (i > 0 &&
                    s[i - 1] == '0')
                    i--;
                             
                // updating the length
                // of the string
                n = s.Length;
            }
        }                
        return s;
    }
     
    // Driver code
    static void Main()
    {
        Console.Write(findstring("100100"));
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)

Javascript

<script>
 
// JavaScript program to delete elements
// between zeros
 
// Function to find the string
// after operation
function findstring(s){
     
    let n = s.length
    s = s.split('')
    let i = 1
 
    // Traversing through string
    while(i < n - 1){
 
        // Checking for character
        // Between two zeros
        if (s[i - 1] == '0' && s[i + 1] == '0'){
 
            // Deleting the character
            // At specific position
            s.splice(i,1);
 
            i -= 1
            if(i > 0 && s[i - 1] == '0')
                i -= 1
 
            // Updating the length
            // of the string
            n = s.length
        }
        i += 1
    }
 
    return s.join('')
}
 
// Driver code
 
document.write(findstring('100100'),"</br>")
 
// This code is contributed by shinjanpatra
 
</script>
Producción: 

100

 

Complejidad de tiempo: O(N), donde N es el tamaño de la string de entrada.
 

Publicación traducida automáticamente

Artículo escrito por Manish_100 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 *