📜  不包含任何元音的最长子串的长度

📅  最后修改于: 2022-05-13 01:57:06.541000             🧑  作者: Mango

不包含任何元音的最长子串的长度

给定一个由N个小写字符组成的字符串S ,任务是找出不包含任何元音的最长子串的长度。

例子:

朴素方法:解决给定问题的最简单方法是生成给定字符串S的所有子字符串,并打印不包含任何元音的最大长度子字符串的长度。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:上述方法也可以使用滑动窗口技术进行优化。
请按照以下步骤解决问题:

  • 初始化两个变量,比如countres0 ,分别存储不带元音的字符串长度和找到的结果子字符串的最大长度。
  • 使用变量i遍历给定的字符串S并执行以下步骤:
    • 如果当前字符S[i]是元音,则将count的值更新为0 。否则,将count的值增加1
    • res 的值更新为 rescount的最大值。
  • 完成上述步骤后,打印res的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


输出:
3

时间复杂度: O(N)
辅助空间: O(1)