Substring más larga que contiene ‘1’

Dada una string binaria , la tarea es imprimir la longitud de la substring más larga que contiene solo ‘1’ .

Ejemplos:

Entrada: 110
Salida: 2
Explicación: La longitud de la substring más larga que contiene solo ‘1’ es «11».

Entrada: 11101110
Salida: 3

Enfoque: Atraviese la string y cuente el número de 1 contiguos encontrados. Almacene el número máximo de 1 s contiguos en una variable, digamos max . Finalmente, imprime el valor de max obtenido.

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

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find length of
// longest substring containing '1'
int maxlength(string s)
{
    int n = s.length(), i, j;
    int ans = 0;
    for (i = 0; i <= n - 1; i++) {
 
        // Count the number of contiguous 1's
        if (s[i] == '1') {
 
            int count = 1;
            for (j = i + 1; j <= n - 1
                            && s[j] == '1';
                 j++)
                count++;
            ans = max(ans, count);
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
    string s = "11101110";
 
    cout << maxlength(s) << endl;
    return 0;
}

Java

// Java program to find length
// of longest substring containing '1'
 
class GFG {
 
    // Function to find length of
    // of longest substring containing '1'
    static int maxlength(String s)
    {
        int n = s.length(), i, j;
        int ans = 0;
        for (i = 0; i <= n - 1; i++) {
 
            // Count the number of contiguous 1's
            if (s.charAt(i) == '1') {
 
                int count = 1;
                for (j = i + 1;
                     j <= n - 1 && s.charAt(j) == '1'; j++)
                    count++;
                ans = Math.max(ans, count);
            }
        }
        return ans;
    }
 
    public static void main(String[] args)
    {
        String s = "11101110";
 
        System.out.println(
            "Length of longest substring containing '1' = "
            + maxlength(s));
    }
}

Python3

# Python program for the above approach
 
# Function to find length of
# longest substring containing '1'
def maxlength(s):
    n = len(s)
    ans = 0;
    for i in range(n):
         
        # Count the number of contiguous 1's
        if (s[i] == '1'):
            count = 1
            j = i + 1
            while(j <= n - 1 and s[j] == '1'):
                count += 1
                j += 1
            ans = max(ans, count)
    return ans
 
# Driver Code
s = "11101110";
print(maxlength(s))
 
# This code is contributed by Shivani

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find length of
// of longest substring containing '1'
static int maxlength(String s)
{
    int n = s.Length, i, j;
    int ans = 0;
     
    for(i = 0; i <= n - 1; i++)
    {
         
        // Count the number of contiguous 1's
        if (s[i] == '1')
        {
            int count = 1;
            for(j = i + 1;
                j <= n - 1 && s[j] == '1';
                j++)
                count++;
                 
            ans = Math.Max(ans, count);
        }
    }
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "11101110";
 
    Console.Write(maxlength(s));
}
}
 
// This code is contributed by shivanisinghss2110

Javascript

<script>
 
        // JavaScript program for the above approach;
 
        // Function to find length of
        // longest substring containing '1'
        function maxlength(s)
        {
            let n = s.length, i, j;
            let ans = 0;
            for (i = 0; i <= n - 1; i++) {
 
                // Count the number of contiguous 1's
                if (s[i] == '1') {
 
                    let count = 1;
                    for (j = i + 1; j <= n - 1
                        && s[j] == '1';
                        j++)
                        count++;
                    ans = Math.max(ans, count);
                }
            }
            return ans;
        }
 
        // Driver Code
        let s = "11101110";
        document.write(maxlength(s));
 
// This code is contributed by Potta Lokesh
    </script>
Producción: 

3

 

Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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