| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.FindTheLongestSubstringContainingVowelsInEvenCounts; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class FindTheLongestSubstringContainingVowelsInEvenCountsBitmasking : |
| | | 16 | | IFindTheLongestSubstringContainingVowelsInEvenCounts |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Time complexity - O(n) |
| | | 20 | | /// Space complexity - O(1) |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="s"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public int FindTheLongestSubstring(string s) |
| | 3 | 25 | | { |
| | 3 | 26 | | var vowelXorValues = new int['z' - 'a' + 1]; |
| | | 27 | | |
| | 3 | 28 | | vowelXorValues['a' - 'a'] = 1; |
| | 3 | 29 | | vowelXorValues['e' - 'a'] = 2; |
| | 3 | 30 | | vowelXorValues['i' - 'a'] = 4; |
| | 3 | 31 | | vowelXorValues['o' - 'a'] = 8; |
| | 3 | 32 | | vowelXorValues['u' - 'a'] = 16; |
| | | 33 | | |
| | 3 | 34 | | var xorStateFirstIndex = new int[32]; |
| | | 35 | | |
| | 198 | 36 | | for (var i = 0; i < 32; i++) |
| | 96 | 37 | | { |
| | 96 | 38 | | xorStateFirstIndex[i] = -1; |
| | 96 | 39 | | } |
| | | 40 | | |
| | 3 | 41 | | var longestSubstringLength = 0; |
| | 3 | 42 | | var currentXorState = 0; |
| | | 43 | | |
| | 82 | 44 | | for (var i = 0; i < s.Length; i++) |
| | 38 | 45 | | { |
| | 38 | 46 | | currentXorState ^= vowelXorValues[s[i] - 'a']; |
| | | 47 | | |
| | 38 | 48 | | if (xorStateFirstIndex[currentXorState] == -1 && currentXorState != 0) |
| | 10 | 49 | | { |
| | 10 | 50 | | xorStateFirstIndex[currentXorState] = i; |
| | 10 | 51 | | } |
| | | 52 | | |
| | 38 | 53 | | longestSubstringLength = Math.Max(longestSubstringLength, i - xorStateFirstIndex[currentXorState]); |
| | 38 | 54 | | } |
| | | 55 | | |
| | 3 | 56 | | return longestSubstringLength; |
| | 3 | 57 | | } |
| | | 58 | | } |