📌  相关文章
📜  没有两个相邻字母相同的元音的最长子串

📅  最后修改于: 2021-09-06 05:10:59             🧑  作者: Mango

给定由小写字母组成的字符串str ,任务是找到最长子字符串的长度,使其所有字符都是元音并且没有两个相邻的字母相同。

例子:

天真的方法:
从给定的字符串生成所有可能的子字符串。对于每个子串,检查它的所有字符是否都是元音并且没有两个相邻的字符是相同的,然后比较所有这些子串的长度并打印最大长度。

下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
// Function to check a
// character is vowel or not
bool isVowel(char c)
{
    return (c == 'a' || c == 'e'
            || c == 'i' || c == 'o'
            || c == 'u');
}
 
// Function to check a
// substring is valid or not
bool isValid(string& s)
{
 
    int n = s.size();
 
    // If size is 1 then
    // check only first character
    if (n == 1)
        return (isVowel(s[0]));
 
    // If 0'th character is
    // not vowel then invalid
    if (isVowel(s[0]) == false)
        return false;
 
    for (int i = 1; i < n; i++) {
 
        // If two adjacent characters
        // are same or i'th char is
        // not vowel then invalid
        if (s[i] == s[i - 1]
            || !isVowel(s[i]))
            return false;
    }
 
    return true;
}
 
// Function to find length
// of longest substring
// consisting only of
// vowels and no similar
// adjacent alphabets
int findMaxLen(string& s)
{
    // Stores max length
    // of valid substring
    int maxLen = 0;
    int n = s.length();
 
    for (int i = 0; i < n; i++) {
 
        // For current substring
        string temp = "";
 
        for (int j = i; j < n; j++) {
 
            temp = temp + s[j];
 
            // Check if substring
            // is valid
            if (isValid(temp))
 
                // Size of substring
                // is (j-i+1)
                maxLen = max(
                    maxLen, (j - i + 1));
        }
    }
 
    return maxLen;
}
 
// Driver code
int main()
{
    string Str = "aeoibsddaeiouudb";
 
    cout << findMaxLen(Str) << endl;
 
    return 0;
}


Java
// Java implementation of
// the above approach
import java.io.*;
class GFG{
 
// Function to check a
// character is vowel or not
static boolean isVowel(char c)
{
    return (c == 'a' || c == 'e' ||
            c == 'i' || c == 'o' ||
            c == 'u');
}
 
// Function to check a
// subString is valid or not
static boolean isValid(String s)
{
    int n = s.length();
 
    // If size is 1 then
    // check only first character
    if (n == 1)
        return (isVowel(s.charAt(0)));
 
    // If 0'th character is
    // not vowel then invalidlen
    if (isVowel(s.charAt(0)) == false)
        return false;
 
    for (int i = 1; i < n; i++)
    {
 
        // If two adjacent characters
        // are same or i'th char is
        // not vowel then invalid
        if (s.charAt(i) == s.charAt(i - 1) ||
                  !isVowel(s.charAt(i)))
            return false;
    }
    return true;
}
 
// Function to find length
// of longest subString
// consisting only of
// vowels and no similar
// adjacent alphabets
static int findMaxLen(String s)
{
     
    // Stores max length
    // of valid subString
    int maxLen = 0;
    int n = s.length();
 
    for (int i = 0; i < n; i++)
    {
 
        // For current subString
        String temp = "";
 
        for (int j = i; j < n; j++)
        {
 
            temp = temp + s.charAt(j);
 
            // Check if subString
            // is valid
            if (isValid(temp))
 
                // Size of subString
                // is (j-i+1)
                maxLen = Math.max(maxLen,
                                 (j - i + 1));
        }
    }
    return maxLen;
}
 
// Driver code
public static void main (String[] args)
{
    String Str = "aeoibsddaeiouudb";
 
    System.out.println(findMaxLen(Str));
}
}
 
// This code is contributed by shubhamcoder


Python3
# Python3 implementation of
# the above approach
 
# Function to check a
# character is vowel or not
def isVowel(c):
    return (c == 'a' or c == 'e' or
            c == 'i' or c == 'o' or
            c == 'u')
 
# Function to check a
# substring is valid or not
def isValid(s):
    n = len(s)
 
    # If size is 1 then
    # check only first character
    if (n == 1):
        return (isVowel(s[0]))
 
    # If 0'th character is
    # not vowel then invalid
    if (isVowel(s[0]) == False):
        return False
 
    for i in range (1, n):
 
        # If two adjacent characters
        # are same or i'th char is
        # not vowel then invalid
        if (s[i] == s[i - 1] or not
            isVowel(s[i])):
            return False
 
    return True
 
# Function to find length
# of longest substring
# consisting only of
# vowels and no similar
# adjacent alphabets
def findMaxLen(s):
 
    # Stores max length
    # of valid substring
    maxLen = 0
    n = len(s)
 
    for i in range (n):
 
        # For current substring
        temp = ""
 
        for j in range (i, n):
            temp = temp + s[j]
 
            # Check if substring
            # is valid
            if (isValid(temp)):
 
                # Size of substring
                # is (j-i+1)
                maxLen = (max(maxLen, (j - i + 1)))
 
    return maxLen
 
# Driver code
if __name__ == "__main__":
 
    Str = "aeoibsddaeiouudb"
    print (findMaxLen(Str))
 
# This code is contributed by Chitranayal


C#
// C# implementation of the above approach
using System;
class GFG{
 
// Function to check a
// character is vowel or not
static bool isVowel(char c)
{
    return (c == 'a' || c == 'e' ||
            c == 'i' || c == 'o' ||
            c == 'u');
}
 
// Function to check a
// substring is valid or not
static bool isValid(string s)
{
 
    int n = s.Length;
 
    // If size is 1 then
    // check only first character
    if (n == 1)
        return (isVowel(s[0]));
 
    // If 0'th character is
    // not vowel then invalid
    if (isVowel(s[0]) == false)
        return false;
 
    for(int i = 1; i < n; i++)
    {
        
       // If two adjacent characters
       // are same or i'th char is
       // not vowel then invalid
       if (s[i] == s[i - 1] ||
          !isVowel(s[i]))
           return false;
    }
    return true;
}
 
// Function to find length
// of longest substring
// consisting only of
// vowels and no similar
// adjacent alphabets
static int findMaxLen(string s)
{
    // Stores max length
    // of valid substring
    int maxLen = 0;
    int n = s.Length;
 
    for(int i = 0; i < n; i++)
    {
        
       // For current substring
       string temp = "";
        
       for(int j = i; j < n; j++)
       {
           temp = temp + s[j];
            
           // Check if substring
           // is valid
           if (isValid(temp))
                
               // Size of substring
               // is (j-i+1)
               maxLen = Math.Max(maxLen,
                                (j - i + 1));
       }
    }
    return maxLen;
}
 
// Driver code
public static void Main()
{
    string Str = "aeoibsddaeiouudb";
 
    Console.WriteLine(findMaxLen(Str));
}
}
 
// This code is contributed by Code_Mech


Javascript


C++
// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
// Function to check a
// character is vowel or not
bool isvowel(char c)
{
    return c == 'a' || c == 'e'
           || c == 'i' || c == 'o'
           || c == 'u';
}
// Function to find length
// of longest substring
// consisting only of
// vowels and no similar
// adjacent alphabets
int findMaxLen(string& s)
{
    // Stores max length
    // of valid subString
    int maxLen = 0;
 
    // Stores length of
    // current valid subString
    int cur = 0;
 
    if (isvowel(s[0]))
        cur = maxLen = 1;
 
    for (int i = 1; i < s.length(); i++) {
        if (isvowel(s[i])) {
            // If curr and prev character
            // are not same, include it
            if (s[i] != s[i - 1])
                cur += 1;
 
            // If same as prev one, start
            // new subString from here
            else
                cur = 1;
        }
 
        else {
            cur = 0;
        }
 
        // Store max in maxLen
        maxLen = max(cur, maxLen);
    }
 
    return maxLen;
}
 
// Driver code
int main()
{
    string Str = "aeoibsddaeiouudb";
    cout << findMaxLen(Str) << endl;
 
    return 0;
}


Java
// Java implementation of
// the above approach
 
public class GFG {
 
    // Function to check a
    // character is vowel or not
    static boolean isVowel(char x)
    {
        return (x == 'a' || x == 'e'
                || x == 'i' || x == 'o'
                || x == 'u');
    }
 
    // Function to find length
    // of longest substring
    // consisting only of
    // vowels and no similar
    // adjacent alphabets
    static int findMaxLen(String s)
    {
        // Stores max length
        // of valid subString
        int maxLen = 0;
 
        // Stores length of
        // current valid subString
        int cur;
 
        if (isVowel(s.charAt(0)))
            maxLen = 1;
 
        cur = maxLen;
 
        for (int i = 1; i < s.length(); i++) {
            if (isVowel(s.charAt(i))) {
                // If curr and prev character
                // are not same, include it
                if (s.charAt(i)
                    != s.charAt(i - 1))
                    cur += 1;
 
                // If same as prev one, start
                // new subString from here
                else
                    cur = 1;
            }
 
            else {
                cur = 0;
            }
 
            // Store max in maxLen
            maxLen = Math.max(cur, maxLen);
        }
 
        return maxLen;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String Str = "aeoibsddaeiouudb";
 
        System.out.println(findMaxLen(Str));
    }
}


Python3
# Python implementation of
# the above approach
  
# Function to check a
# character is vowel or not
def isVowel(x):
       
    return (x == 'a' or x == 'e' or
            x == 'i' or x == 'o' or
            x == 'u');
   
# Function to find length
# of longest substring
# consisting only of
# vowels and no similar
# adjacent alphabets
def findMaxLen(s):
  
    # Stores max length
    # of valid subString
    maxLen = 0
     
    # Stores length of
    # current valid subString
    cur = 0
      
    if(isVowel(s[0])):
        maxLen = 1;
          
    cur = maxLen
      
    for i in range(1, len(s)):
          
        if(isVowel(s[i])):
            # If curr and prev character
            # are not same, include it
            if(s[i] != s[i-1]):
                cur += 1
              
            # If same as prev one, start
            # new subString from here   
            else:
                cur = 1
          
        else:
            cur = 0;
          
        # Store max in maxLen
        maxLen = max(cur, maxLen);
      
    return maxLen
   
# Driver code
  
Str = "aeoibsddaeiouudb"
      
print(findMaxLen(Str))


C#
// C# implementation of
// the above approach
 
using System;
 
public class GFG {
 
    // Function to check a
    // character is vowel or not
    public static bool isVowel(char x)
    {
        return (x == 'a' || x == 'e'
                || x == 'i' || x == 'o'
                || x == 'u');
    }
 
    // Function to find length
    // of longest substring
    // consisting only of
    // vowels and no similar
    // adjacent alphabets
    public static int findMaxLen(string s)
    {
        // Stores max length
        // of valid subString
        int maxLen = 0;
 
        // Stores length of
        // current valid subString
        int cur;
 
        if (isVowel(s[0]))
            maxLen = 1;
 
        cur = maxLen;
 
        for (int i = 1; i < s.Length; i++) {
            if (isVowel(s[i])) {
                // If curr and prev character
                // are not same, include it
                if (s[i] != s[i - 1])
                    cur += 1;
 
                // If same as prev one, start
                // new subString from here
                else
                    cur = 1;
            }
 
            else {
                cur = 0;
            }
 
            // Store max in maxLen
            maxLen = Math.Max(cur, maxLen);
        }
 
        return maxLen;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        string Str = "aeoibsddaeiouudb";
 
        Console.WriteLine(findMaxLen(Str));
    }
}


Javascript


输出:
5

时间复杂度: O(N 3 )

有效的方法:
对于这个问题,线性时间解决方案是可能的。如果一个子串的所有字符都是元音并且没有两个相邻的字符相同,则该子串将被认为是有效的。这个想法是遍历字符串并跟踪最长的有效子字符串。
以下是详细步骤:

  1. 创建一个变量cur来存储当前有效子字符串的长度,创建另一个变量maxVal来跟踪迄今为止发现的最大cur ,最初,两者都设置为 0。
  2. 如果索引 0 处的字符是元音,则它是长度为 1 的有效子串,因此 maxVal = 1
  3. 从索引 1 开始遍历str 。如果当前字符不是元音,则不存在有效子串,cur = 0
  4. 如果当前字符是元音并且
    • 与前一个字符不同,然后将其包含在当前有效子字符串中,并将cur增加 1。更新maxVal
    • 与前一个字符相同,则新的有效子字符串从此字符开始, cur重置为 1。
  5. maxVal的最终值给出了答案。

下面是上述方法的实现:

C++

// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
// Function to check a
// character is vowel or not
bool isvowel(char c)
{
    return c == 'a' || c == 'e'
           || c == 'i' || c == 'o'
           || c == 'u';
}
// Function to find length
// of longest substring
// consisting only of
// vowels and no similar
// adjacent alphabets
int findMaxLen(string& s)
{
    // Stores max length
    // of valid subString
    int maxLen = 0;
 
    // Stores length of
    // current valid subString
    int cur = 0;
 
    if (isvowel(s[0]))
        cur = maxLen = 1;
 
    for (int i = 1; i < s.length(); i++) {
        if (isvowel(s[i])) {
            // If curr and prev character
            // are not same, include it
            if (s[i] != s[i - 1])
                cur += 1;
 
            // If same as prev one, start
            // new subString from here
            else
                cur = 1;
        }
 
        else {
            cur = 0;
        }
 
        // Store max in maxLen
        maxLen = max(cur, maxLen);
    }
 
    return maxLen;
}
 
// Driver code
int main()
{
    string Str = "aeoibsddaeiouudb";
    cout << findMaxLen(Str) << endl;
 
    return 0;
}

Java

// Java implementation of
// the above approach
 
public class GFG {
 
    // Function to check a
    // character is vowel or not
    static boolean isVowel(char x)
    {
        return (x == 'a' || x == 'e'
                || x == 'i' || x == 'o'
                || x == 'u');
    }
 
    // Function to find length
    // of longest substring
    // consisting only of
    // vowels and no similar
    // adjacent alphabets
    static int findMaxLen(String s)
    {
        // Stores max length
        // of valid subString
        int maxLen = 0;
 
        // Stores length of
        // current valid subString
        int cur;
 
        if (isVowel(s.charAt(0)))
            maxLen = 1;
 
        cur = maxLen;
 
        for (int i = 1; i < s.length(); i++) {
            if (isVowel(s.charAt(i))) {
                // If curr and prev character
                // are not same, include it
                if (s.charAt(i)
                    != s.charAt(i - 1))
                    cur += 1;
 
                // If same as prev one, start
                // new subString from here
                else
                    cur = 1;
            }
 
            else {
                cur = 0;
            }
 
            // Store max in maxLen
            maxLen = Math.max(cur, maxLen);
        }
 
        return maxLen;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String Str = "aeoibsddaeiouudb";
 
        System.out.println(findMaxLen(Str));
    }
}

蟒蛇3

# Python implementation of
# the above approach
  
# Function to check a
# character is vowel or not
def isVowel(x):
       
    return (x == 'a' or x == 'e' or
            x == 'i' or x == 'o' or
            x == 'u');
   
# Function to find length
# of longest substring
# consisting only of
# vowels and no similar
# adjacent alphabets
def findMaxLen(s):
  
    # Stores max length
    # of valid subString
    maxLen = 0
     
    # Stores length of
    # current valid subString
    cur = 0
      
    if(isVowel(s[0])):
        maxLen = 1;
          
    cur = maxLen
      
    for i in range(1, len(s)):
          
        if(isVowel(s[i])):
            # If curr and prev character
            # are not same, include it
            if(s[i] != s[i-1]):
                cur += 1
              
            # If same as prev one, start
            # new subString from here   
            else:
                cur = 1
          
        else:
            cur = 0;
          
        # Store max in maxLen
        maxLen = max(cur, maxLen);
      
    return maxLen
   
# Driver code
  
Str = "aeoibsddaeiouudb"
      
print(findMaxLen(Str))

C#

// C# implementation of
// the above approach
 
using System;
 
public class GFG {
 
    // Function to check a
    // character is vowel or not
    public static bool isVowel(char x)
    {
        return (x == 'a' || x == 'e'
                || x == 'i' || x == 'o'
                || x == 'u');
    }
 
    // Function to find length
    // of longest substring
    // consisting only of
    // vowels and no similar
    // adjacent alphabets
    public static int findMaxLen(string s)
    {
        // Stores max length
        // of valid subString
        int maxLen = 0;
 
        // Stores length of
        // current valid subString
        int cur;
 
        if (isVowel(s[0]))
            maxLen = 1;
 
        cur = maxLen;
 
        for (int i = 1; i < s.Length; i++) {
            if (isVowel(s[i])) {
                // If curr and prev character
                // are not same, include it
                if (s[i] != s[i - 1])
                    cur += 1;
 
                // If same as prev one, start
                // new subString from here
                else
                    cur = 1;
            }
 
            else {
                cur = 0;
            }
 
            // Store max in maxLen
            maxLen = Math.Max(cur, maxLen);
        }
 
        return maxLen;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        string Str = "aeoibsddaeiouudb";
 
        Console.WriteLine(findMaxLen(Str));
    }
}

Javascript


输出:
5

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live