diff --git a/solving/count-vowel-substrings-of-a-string/main.cpp b/solving/count-vowel-substrings-of-a-string/main.cpp index 904aaa1..1320252 100644 --- a/solving/count-vowel-substrings-of-a-string/main.cpp +++ b/solving/count-vowel-substrings-of-a-string/main.cpp @@ -91,9 +91,49 @@ typedef vector vi; #define pb push_back class Solution { +private: + int toIdx(char x) { + if (x == 'a') return 0; + if (x == 'e') return 1; + if (x == 'i') return 2; + if (x == 'o') return 3; + if (x == 'u') return 4; + return 5; + } + + int minLast(int last[5]) { + int res = last[0]; + for (int i = 1; i < 5; i++) res = min(last[i], res); + return res; + } + public: int countVowelSubstrings(string word) { - + int n = word.size(); + int res = 0; + int start = 0; + + int last[5]; + memset(last, -1, sizeof(last)); + + for (int j = 0; j < n; j++) { + int idx = toIdx(word[j]); + + // Have const + if (idx > 4) { + start = j + 1; + memset(last, -1, sizeof(last)); + continue; + } + + last[idx] = j; + int minLst = minLast(last); + + if (minLst != -1) { + res += minLst - start + 1; + } + } + return res; } };