Longitud de la substring más larga que no contiene ninguna vocal

Dada una string S que consta de N caracteres en minúscula, la tarea es encontrar la longitud de la substring más larga que no contiene ninguna vocal.

Ejemplos:

Entrada: S = “geeksforgeeks”
Salida: 3
La substring “ksf” es la substring más larga que no contiene ninguna vocal. La longitud de esta substring es 3.

Entrada: S = “ceebbaceeffo”
Salida: 2

Enfoque ingenuo: el enfoque más simple para resolver el problema dado es generar todas las substrings de la string S dada e imprimir la longitud de la substring de longitud máxima que no contiene ninguna vocal. 

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

Enfoque eficiente: el enfoque anterior también se puede optimizar utilizando la técnica de ventana deslizante
Siga los pasos a continuación para resolver el problema: 

  • Inicialice dos variables, digamos count y res como 0 , para almacenar la longitud de la string sin ninguna vocal y la longitud máxima de la substring resultante encontrada respectivamente.
  • Recorra la string S dada usando la variable i y realice los siguientes pasos:
  • Después de completar los pasos anteriores, imprima el valor de res como resultado.

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 check if the
// character is a vowel or not
bool vowel(char ch)
{
    if (ch == 'a' || ch == 'e'
        || ch == 'i' || ch == 'o'
        || ch == 'u' || ch == 'A'
        || ch == 'E' || ch == 'I'
        || ch == 'O' || ch == 'U') {
        return true;
    }
    return false;
}
 
// Function to find the length of
// the longest substring that
// doesn't contain any vowel
int maxLengthString(string s)
{
    // Stores the length of
    // the longest substring
    int maximum = 0;
 
    int count = 0;
 
    // Traverse the string, S
    for (int i = 0; i < s.length(); i++) {
 
        // If the current character
        // is vowel, set count as 0
        if (vowel(s[i])) {
            count = 0;
        }
 
        // If the current
        // character is a consonant
        else {
 
            // Increment count by 1
            count++;
        }
 
        // Update the maximum length
        maximum = max(maximum, count);
    }
 
    // Return the result
    return maximum;
}
 
// Driver Code
int main()
{
    string S = "geeksforgeeks";
    cout << maxLengthString(S);
 
    return 0;
}

Java

// Java program for the above approach
 
import java.io.*;
 
class GFG {
    public static boolean vowel(char ch)
    {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
            || ch == 'u' || ch == 'A' || ch == 'E'
            || ch == 'I' || ch == 'O' || ch == 'U') {
            return true;
        }
        return false;
    }
 
    // Function to find the length of
    // the longest substring that
    // doesn't contain any vowel
    public static int maxLengthString(String s)
    {
        // Stores the length of
        // the longest substring
        int maximum = 0;
 
        int count = 0;
 
        // Traverse the string, S
        for (int i = 0; i < s.length(); i++) {
 
            // If the current character
            // is vowel, set count as 0
            if (vowel(s.charAt(i))) {
                count = 0;
            }
 
            // If the current
            // character is a consonant
            else {
 
                // Increment count by 1
                count++;
            }
 
            // Update the maximum length
            maximum = Math.max(maximum, count);
        }
 
        // Return the result
        return maximum;
    }
 
    public static void main(String[] args)
    {
        String S = "geeksforgeeks";
        System.out.println(maxLengthString(S));
      // This code is contributed by Potta Lokesh
    }

Python

# Python program for the above approach
# Function to check if the
# character is a vowel or not
def vowel(ch):
 
    if (ch == 'a' or ch == 'e'
        or ch == 'i' or ch == 'o'
        or ch == 'u' or ch == 'A'
        or ch == 'E' or ch == 'I'
        or ch == 'O' or ch == 'U'):
        return True
     
        return False
 
# Function to find the length of
# the longest substring that
# doesn't contain any vowel
def maxLengthString(s):
   
    # Stores the length of
    # the longest substring
    maximum = 0  
    count = 0;
     
    # Traverse the string, S
    for i in range(len(s)):
         
        # If the current character
        # is vowel, set count as 0
        if (vowel(s[i])):
            count = 0;
         
        # If the current
        # character is a consonant
        else:
            # Increment count by 1
            count += 1
             
        # Update the maximum length
        maximum = max(maximum, count)
         
    # Return the result
    return maximum
 
# Driver Code
S = 'geeksforgeeks'
print(maxLengthString(S))
 
# This code is contributed by shivanisinghss2110

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check if the
// character is a vowel or not
static bool vowel(char ch)
{
    if (ch == 'a' || ch == 'e' ||
        ch == 'i' || ch == 'o' ||
        ch == 'u' || ch == 'A' ||
        ch == 'E' || ch == 'I' ||
        ch == 'O' || ch == 'U')
    {
        return true;
    }
    return false;
}
 
// Function to find the length of
// the longest substring that
// doesn't contain any vowel
static int maxLengthString(string s)
{
     
    // Stores the length of
    // the longest substring
    int maximum = 0;
 
    int count = 0;
 
    // Traverse the string, S
    for(int i = 0; i < s.Length; i++)
    {
         
        // If the current character
        // is vowel, set count as 0
        if (vowel(s[i]) == true)
        {
            count = 0;
        }
 
        // If the current
        // character is a consonant
        else
        {
             
            // Increment count by 1
            count++;
        }
 
        // Update the maximum length
        maximum = Math.Max(maximum, count);
    }
 
    // Return the result
    return maximum;
}
 
// Driver Code
public static void Main()
{
    string S = "geeksforgeeks";
     
    Console.Write(maxLengthString(S));
}
}
 
// This code is contributed by SURENDRA_GANGWAR

Javascript

<script>
 
        // JavaScript program for the above approach
 
        // Function to check if the
        // character is a vowel or not
        function vowel(ch) {
            if (ch == 'a' || ch == 'e'
                || ch == 'i' || ch == 'o'
                || ch == 'u' || ch == 'A'
                || ch == 'E' || ch == 'I'
                || ch == 'O' || ch == 'U') {
                return true;
            }
            return false;
        }
 
        // Function to find the length of
        // the longest substring that
        // doesn't contain any vowel
        function maxLengthString(s) {
            // Stores the length of
            // the longest substring
            let maximum = 0;
 
            let count = 0;
 
            // Traverse the string, S
            for (let i = 0; i < s.length; i++) {
 
                // If the current character
                // is vowel, set count as 0
                if (vowel(s[i])) {
                    count = 0;
                }
 
                // If the current
                // character is a consonant
                else {
 
                    // Increment count by 1
                    count++;
                }
 
                // Update the maximum length
                maximum = Math.max(maximum, count);
            }
 
            // Return the result
            return maximum;
        }
 
        // Driver Code
 
        var S = "geeksforgeeks";
        document.write(maxLengthString(S));
 
// This code is contributed by Potta Lokesh
 
</script>
Producción: 

3

 

Complejidad temporal: O(N)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

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