📜  包含偶数个元音的最长子串的长度

📅  最后修改于: 2021-10-27 08:16:27             🧑  作者: Mango

给定一个由N个小写字符组成的字符串S ,任务是找到由偶数个元音组成的最长子串的长度。

例子:

朴素的方法:解决给定问题的最简单方法是从给定的字符串S生成所有可能的子串,并且对于每个子串,检查子串中所有元音的频率是否为偶数。如果发现为true ,则更新所需字符串的最大长度。检查所有子字符串后,打印获得的最大长度。

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

高效的方法:上述方法也可以通过使用Hashing进行优化。这个想法是初始化一个字符串,比如大小为5 的temp对应于全05个元音 (a, e, i, o, u) ,其中s[i] = 0表示第i元音出现偶数次数。现在,遍历定的字符串,找到字符串,临时从地图相同的状态和更新的最大长度。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0来存储所需的结果。
  • 初始化一个字符串,将大小为5 的temp设为“00000”
  • 创建一个 hashmap, M来存储字符串temp出现的索引,并将tempM 中的值初始化为-1
  • 使用变量i[0, N – 1]范围内遍历给定字符串S并执行以下步骤:
    • 如果字符S[i]是元音,则更新字符串temp
    • 如果字符串temp存在于映射M 中,则将其来自映射M 的值存储在变量X 中,并将ans的值更新为ans(i – X)的最大值。
    • 否则,将映射M 中temp值更新为i
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the length of the
// longest substring having even number
// of vowels
int longestSubstring(string s)
{
    // Create two hashmaps
    unordered_map indexes;
    unordered_map chars(
        { { 'a', 0 }, { 'e', 1 },
          { 'i', 2 }, { 'o', 3 },
          { 'u', 4 } });
 
    // Keep the track of frequencies
    // of the vowels
    string evenOdd = "00000";
    indexes[evenOdd] = -1;
 
    // Stores the maximum length
    int length = 0;
 
    // Traverse the given string S
    for (int i = 0; i < s.size(); ++i) {
 
        char c = s[i];
 
        // Find character in the map
        auto it = chars.find(c);
 
        // If it is a vowel, then update
        // the frequency
        if (it != chars.end()) {
            evenOdd[it->second]
                = evenOdd[it->second]
                          == '0'
                      ? '1'
                      : '0';
        }
 
        // Find the index of occurence
        // of the string evenOdd in map
        auto lastIndex = indexes.find(evenOdd);
 
        if (lastIndex == indexes.end()) {
            indexes[evenOdd] = i;
        }
 
        // Update the maximum length
        else {
            length = max(
                length, i - lastIndex->second);
        }
    }
 
    // Print the maximum length
    cout << length;
}
 
// Driver Code
int main()
{
    string S = "bcbcbc";
    longestSubstring(S);
 
    return 0;
}


Python3
# Python3 program for the above approach
 
# Function to find the length of the
# longest substring having even number
# of vowels
def longestSubstring(s):
     
    # Create two hashmaps
    indexes = {}
    chars = {}
    chars['a'] = 0
    chars['e'] = 1
    chars['i'] = 2
    chars['o'] = 3
    chars['u'] = 4
     
    # Keep the track of frequencies
    # of the vowels
    evenOdd = "00000"
    evenOdd = [i for i in evenOdd]
    indexes["".join(evenOdd)] = -1
 
    # Stores the maximum length
    length = 0
 
    # Traverse the given string S
    for i in range(len(s)):
        c = s[i]
 
        # Find character in the map
 
        # If it is a vowel, then update
        # the frequency
        if (c in chars):
            evenOdd[chars[it]] = '1' if evenOdd[chars[it]] else '0'
             
        # Find the index of occurence
        # of the string evenOdd in map
        if ("".join(evenOdd) not in indexes):
            indexes["".join(evenOdd)] = i
             
        # Update the maximum length
        else:
            length = max(
                length, i - indexes["".join(evenOdd)])
 
    # Print the maximum length
    print(length)
 
# Driver Code
if __name__ == '__main__':
     
    S = "bcbcbc"
     
    longestSubstring(S)
 
# This code is contributed by mohit kumar 29


输出:
6

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程