-
Notifications
You must be signed in to change notification settings - Fork 20
Solution #3306 - Mridul/Edited - 11/03/2025 #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mrid88
wants to merge
2
commits into
Dijkstra-Edu:master
Choose a base branch
from
mrid88:feature/count-of-substrings-vowels
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...- Count of Substrings Containing Every Vowel and K Consonants II/Explanation.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
37 changes: 37 additions & 0 deletions
37
...ions/#3306 - Count of Substrings Containing Every Vowel and K Consonants II/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<n;right++) | ||
{ | ||
char ch=word.charAt(right); | ||
if(isConsonant(ch)) currConsonant++; | ||
freq[ch-'a']++; | ||
while(currConsonant>=k && isAllVowelPresent(freq)) | ||
{ | ||
cnt+=(n-right); | ||
char c=word.charAt(left); | ||
if(isConsonant(c)) currConsonant--; | ||
freq[c-'a']--; | ||
left++; | ||
} | ||
} | ||
return cnt; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you utilize the markdown like your other PR's? Also add time and space complexity + how this could be further optimized (if possible)