aboutsummaryrefslogtreecommitdiff
path: root/count_substr_vowles_2.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-09 23:35:52 -0600
committerJack Sangdahl <[email protected]>2025-03-09 23:35:52 -0600
commitcd3a0671a09d5e70567c2246a79018336f5680ac (patch)
treed2c6a10da076cbaa069a803283ce2dc3682414c8 /count_substr_vowles_2.c
parent9a3c2c66ce7572fc2ed8d502fc1f75438e47cf7d (diff)
downloadleetcode-cd3a0671a09d5e70567c2246a79018336f5680ac.tar.gz
leetcode-cd3a0671a09d5e70567c2246a79018336f5680ac.zip
3306. count of substrings containing every vowel and k consonants 2
Diffstat (limited to 'count_substr_vowles_2.c')
-rw-r--r--count_substr_vowles_2.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/count_substr_vowles_2.c b/count_substr_vowles_2.c
new file mode 100644
index 0000000..fd0be13
--- /dev/null
+++ b/count_substr_vowles_2.c
@@ -0,0 +1,42 @@
+// 3306. Count of Substrings Containing Every Vowel and K Consonants II
+#include "leetcode.h"
+
+/*
+ * you are given a string 'word' and a non-negative integer 'k'. return the
+ * total number of substrings of 'word' that contain every vowel at least once
+ * and exactly 'k' constants
+ */
+
+long long countOfSubstrings(char *word, int k) {
+ int freq[2][128] = {{0}};
+ freq[0]['a'] = freq[0]['e'] = freq[0]['i'] = freq[0]['o'] = freq[0]['u'] = 1;
+ long long ans = 0;
+ int curr = 0, cnt = 0, extra = 0, left = 0;
+ for (int right = 0; word[right]; right++) {
+ char r_char = word[right];
+ if (freq[0][r_char]) {
+ if (++freq[1][r_char] == 1)
+ cnt++;
+ } else
+ curr++;
+ while (curr > k) {
+ char l_char = word[left++];
+ if (freq[0][l_char]) {
+ if (--freq[1][l_char] == 0)
+ cnt--;
+ } else
+ curr--;
+ extra = 0;
+ }
+ if (curr == k && cnt == 5)
+ ans += (extra + 1);
+ }
+ return ans;
+}
+
+int main() {
+ char *w1 = "aeioqq", *w2 = "aeiou", *w3 = "ieaouqqieaouqq";
+ printf("%lld\n", countOfSubstrings(w1, 1)); // expect: 0
+ printf("%lld\n", countOfSubstrings(w2, 0)); // expect: 1
+ printf("%lld\n", countOfSubstrings(w3, 1)); // expect: 3
+}