Find the Longest Substring Containing Vowels in Even Counts in C++

The problem of finding the longest substring with vowels appearing an even number of times can be solved efficiently using bit manipulation and prefix sums. We need to track the parity (odd/even count) of each vowel ('a', 'e', 'i', 'o', 'u') as we traverse the string.

Syntax

int findLongestSubstring(char* s);

Algorithm Steps

The approach uses a bitmask to represent the parity state of vowels −

  • Use a 5-bit mask where each bit represents parity of vowels a, e, i, o, u
  • Store the first occurrence of each mask state
  • When a mask repeats, calculate the substring length between occurrences
  • The substring between two identical mask states will have even vowel counts

Example

Here's the complete C implementation −

#include <stdio.h>
#include <string.h>

int findLongestSubstring(char* s) {
    int len = strlen(s);
    int maxLen = 0;
    int mask = 0;
    
    // Array to store first occurrence of each mask (-1 means not seen)
    int firstOccurrence[32]; // 2^5 = 32 possible masks
    for (int i = 0; i < 32; i++) {
        firstOccurrence[i] = -2; // Initialize with -2
    }
    firstOccurrence[0] = -1; // Empty prefix has mask 0
    
    for (int i = 0; i < len; i++) {
        char c = s[i];
        
        // Update mask based on vowel
        switch(c) {
            case 'a': mask ^= 1; break;      // bit 0
            case 'e': mask ^= 2; break;      // bit 1  
            case 'i': mask ^= 4; break;      // bit 2
            case 'o': mask ^= 8; break;      // bit 3
            case 'u': mask ^= 16; break;     // bit 4
        }
        
        if (firstOccurrence[mask] != -2) {
            // We've seen this mask before
            int currentLen = i - firstOccurrence[mask];
            if (currentLen > maxLen) {
                maxLen = currentLen;
            }
        } else {
            // First time seeing this mask
            firstOccurrence[mask] = i;
        }
    }
    
    return maxLen;
}

int main() {
    char s1[] = "helloworld";
    char s2[] = "leetcode";
    char s3[] = "bcbcbc";
    
    printf("Input: "%s"\n", s1);
    printf("Output: %d\n\n", findLongestSubstring(s1));
    
    printf("Input: "%s"\n", s2);
    printf("Output: %d\n\n", findLongestSubstring(s2));
    
    printf("Input: "%s"\n", s3);
    printf("Output: %d\n", findLongestSubstring(s3));
    
    return 0;
}
Input: "helloworld"
Output: 8

Input: "leetcode"
Output: 6

Input: "bcbcbc"
Output: 6

How It Works

The algorithm uses XOR operations to toggle bits representing vowel parity −

  • Mask 0: All vowels appear even times (including zero)
  • Same mask at positions i and j: Substring from i+1 to j has even vowel counts
  • XOR property: XOR of even number of 1's is 0, odd number is 1

Time and Space Complexity

Aspect Complexity Explanation
Time O(n) Single pass through string
Space O(1) Fixed array of size 32

Conclusion

This bit manipulation approach efficiently finds the longest substring with even vowel counts in linear time. The key insight is using XOR to track parity states and finding identical states that indicate valid substrings.

Updated on: 2026-03-15T12:46:49+05:30

408 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements