From bcf239ccb5084ac6fff44c5bf398fc1a402c1bb6 Mon Sep 17 00:00:00 2001 From: mrid88 Date: Tue, 11 Mar 2025 20:36:09 +0530 Subject: [PATCH] Solution #3306 - Mridul/Edited - 11/03/2025 --- .../Explanation.md | 33 +++++++++++++++++ .../Solution.java | 37 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 Daily Questions/#3306 - Count of Substrings Containing Every Vowel and K Consonants II/Explanation.md create mode 100644 Daily Questions/#3306 - Count of Substrings Containing Every Vowel and K Consonants II/Solution.java diff --git a/Daily Questions/#3306 - Count of Substrings Containing Every Vowel and K Consonants II/Explanation.md b/Daily Questions/#3306 - Count of Substrings Containing Every Vowel and K Consonants II/Explanation.md new file mode 100644 index 0000000..d55e750 --- /dev/null +++ b/Daily Questions/#3306 - Count of Substrings Containing Every Vowel and K Consonants II/Explanation.md @@ -0,0 +1,33 @@ +Initialization: + +n: Length of the word. + +freq: Array to store the frequency of each character. + +currConsonant: Counter for the current number of consonants in the substring. + +cnt: Counter for the valid substrings. + +left: Left pointer for the sliding window. + +Iterating through the word: + +For each character ch in the word: + +If ch is a consonant, increment currConsonant. + +Increment the frequency of ch in freq. + +Checking the conditions: + +While the current substring contains at least k consonants and all vowels: + +Increment cnt by the number of valid substrings ending at the current position. + +Move the left pointer to reduce the window size. + +Decrement the frequency of the character at the left pointer. + +If the character is a consonant, decrement currConsonant. + +This logic ensures that we count all substrings that meet the conditions specified in the problem. \ No newline at end of file diff --git a/Daily Questions/#3306 - Count of Substrings Containing Every Vowel and K Consonants II/Solution.java b/Daily Questions/#3306 - Count of Substrings Containing Every Vowel and K Consonants II/Solution.java new file mode 100644 index 0000000..91de701 --- /dev/null +++ b/Daily Questions/#3306 - Count of Substrings Containing Every Vowel and K Consonants II/Solution.java @@ -0,0 +1,37 @@ +class Solution { + public long countOfSubstrings(String word, int k) { + return atleast(k, word)-atleast(k+1, word); + } + private boolean isAllVowelPresent(int[] freq) + { + return (freq['a'-'a']>0 && freq['e'-'a']>0 && freq['i'-'a']>0 && + freq['o'-'a']>0 && freq['u'-'a']>0); + } + private boolean isConsonant(char ch) + { + return (ch!='a' && ch!='e' && ch!='i' && ch!='o' && ch!='u'); + } + private long atleast(int k, String word) + { + int n=word.length(); + int[] freq=new int[26]; + int currConsonant=0; + long cnt=0; + int left=0; + for(int right=0;right=k && isAllVowelPresent(freq)) + { + cnt+=(n-right); + char c=word.charAt(left); + if(isConsonant(c)) currConsonant--; + freq[c-'a']--; + left++; + } + } + return cnt; + } +}