Comprobar si una string tiene m 1 o 0 consecutivos

Dada una string binaria y un número m, la tarea es verificar si la string tiene m 1 o 0 consecutivos.

Ejemplos: 

Entrada: str = “001001”, m = 2 
Salida:

Entrada: str = “1000000001”, m = 10 
Salida: NO 

El enfoque es contar los 1 o 0 consecutivos recorriendo la string binaria. Mientras atraviesa la string binaria, lleve la cuenta del número de 1 o 0 que aparecen consecutivamente. Si hay M 1 o 0 consecutivos, devuelve True , de lo contrario, devuelve False .

A continuación se muestra la implementación del enfoque anterior: 

C++

// Program to check if the binary string
// contains m consecutive 1's or 0's
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
 
// Function that checks if
// the binary string contains m
// consecutive 1's or 0's
bool check(string s, int m)
{
    // length of binary string
    int l = s.length();
 
    // counts zeros
    int c1 = 0;
 
    // counts 1's
    int c2 = 0;
 
    for (int i = 0; i < l; i++) {
 
        if (s[i] == '0') {
            c2 = 0;
            
           // count consecutive 0's
            c1++;
        }
        else {
            c1 = 0;
 
            // count consecutive 1's
            c2++;
        }
        if (c1 == m || c2 == m)
            return true;
    }
    return false;
}
 
// Drivers Code
int main()
{
    string s = "001001";
    int m = 2;
 
    // function call
    if (check(s, m))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}

Java

// Program to check if the
// binary string contains
// m consecutive 1's or 0's
import java.io.*;
 
class GFG
{
 
// Function that checks if
// the binary string contains m
// consecutive 1's or 0's
static boolean check(String s,
                     int m)
{
    // length of binary string
    int l = s.length();
 
    // counts zeros
    int c1 = 0;
 
    // counts 1's
    int c2 = 0;
 
    for (int i = 0; i < l; i++)
    {
 
        if (s.charAt(i) == '0')
        {
            c2 = 0;
             
        // count consecutive 0's
            c1++;
        }
        else
        {
            c1 = 0;
 
            // count consecutive 1's
            c2++;
        }
        if (c1 == m || c2 == m)
            return true;
    }
    return false;
}
 
// Drivers Code
 
public static void main (String[] args)
{
    String s = "001001";
    int m = 2;
     
    // function call
    if (check(s, m))
        System.out.println( "YES");
    else
        System.out.println( "NO");
}
}
 
// This code is contributed by anuj_67.

Python 3

# Program to check if the binary string
# contains m consecutive 1's or 0's
 
# Function that checks if
# the binary string contains m
# consecutive 1's or 0's
def check(s, m):
 
    # length of binary string
    l = len(s);
 
    # counts zeros
    c1 = 0;
 
    # counts 1's
    c2 = 0;
 
    for i in range(0, l - 1):
 
        if (s[i] == '0'):
            c2 = 0;
             
        # count consecutive 0's
            c1 = c1 + 1;
         
        else :
            c1 = 0;
 
            # count consecutive 1's
            c2 = c2 + 1;
         
        if (c1 == m or c2 == m):
            return True;
     
    return False;
 
# Driver Code
s = "001001";
m = 2;
 
# function call
if (check(s, m)):
    print("YES");
else :
    print("NO");
 
# This code is contributed
# by Shivi_Agggarwal

C#

// Program to check if the
// binary string contains
// m consecutive 1's or 0's
using System;
 
class GFG
{
 
// Function that checks if
// the binary string contains
// m consecutive 1's or 0's
static bool check(string s,
                  int m)
{
    // length of
    // binary string
    int l = s.Length;
 
    // counts zeros
    int c1 = 0;
 
    // counts 1's
    int c2 = 0;
 
    for (int i = 0; i < l; i++)
    {
 
        if (s[i] == '0')
        {
            c2 = 0;
             
            // count consecutive
            // 0's
            c1++;
        }
        else
        {
            c1 = 0;
 
            // count consecutive
            // 1's
            c2++;
        }
        if (c1 == m || c2 == m)
            return true;
    }
    return false;
}
 
// Driver Code
public static void Main ()
{
    String s = "001001";
    int m = 2;
     
    // function call
    if (check(s, m))
        Console.WriteLine( "YES");
    else
        Console.WriteLine( "NO");
}
}
 
// This code is contributed
// by anuj_67.

PHP

<?php
// Program to check if the
// binary string contains m
// consecutive 1's or 0's
 
// Function that checks if
// the binary string contains
// m consecutive 1's or 0's
function check($s, $m)
{
    // length of binary
    // string
    $l = count($s);
 
    // counts zeros
    $c1 = 0;
 
    // counts 1's
    $c2 = 0;
 
    for ($i = 0; $i <= $l; $i++)
    {
 
        if ($s[$i] == '0')
        {
            $c2 = 0;
             
            // count consecutive
            // 0's
            $c1++;
        }
        else
        {
            $c1 = 0;
 
            // count consecutive 1's
            $c2++;
        }
        if ($c1 == $m or
            $c2 == $m)
            return true;
    }
    return false;
}
 
// Driver Code
$s = "001001";
$m = 2;
 
// function call
if (check($s, $m))
    echo "YES";
else
    echo "NO";
 
// This code is contributed
// by anuj_67.
?>

Javascript

<script>
    // Program to check if the
    // binary string contains
    // m consecutive 1's or 0's
     
    // Function that checks if
    // the binary string contains
    // m consecutive 1's or 0's
    function check(s, m)
    {
        // length of
        // binary string
        let l = s.length;
 
        // counts zeros
        let c1 = 0;
 
        // counts 1's
        let c2 = 0;
 
        for (let i = 0; i < l; i++)
        {
 
            if (s[i] == '0')
            {
                c2 = 0;
 
                // count consecutive
                // 0's
                c1++;
            }
            else
            {
                c1 = 0;
 
                // count consecutive
                // 1's
                c2++;
            }
            if (c1 == m || c2 == m)
                return true;
        }
        return false;
    }
     
    let s = "001001";
    let m = 2;
       
    // function call
    if (check(s, m))
        document.write( "YES");
    else
        document.write( "NO");
     
</script>
Producción : 

YES

 

Complejidad de tiempo: O(N), donde N es la longitud de la string binaria. 
 

Publicación traducida automáticamente

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