-
Notifications
You must be signed in to change notification settings - Fork 20
Solution #1358 - Mridul/Edited - 11/03/2025 #26
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Solution Explanation | ||
|
||
## Purpose | ||
The code is designed to count the number of substrings that contain all three characters 'a', 'b', and 'c' at least once. | ||
|
||
## Code Breakdown | ||
|
||
### Initialization | ||
```java | ||
HashMap<Character, Integer> freq = new HashMap<>(); | ||
int left = 0, cnt = 0; | ||
|
||
Time complexity: O(n) | ||
Space complexity: O(n) | ||
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kindly explain the Time & Space Complexity as well |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
public int numberOfSubstrings(String s) { | ||
HashMap<Character, Integer> freq = new HashMap<>(); | ||
int left = 0, cnt = 0; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
freq.put(s.charAt(i), freq.getOrDefault(s.charAt(i), 0) + 1); | ||
|
||
while (freq.size() == 3) { | ||
cnt += s.length() - i; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to use a more meaningful variable name. This is fine here, but it would be better to call it count rather than cnt. It's a small pointer and can be ignored |
||
freq.put(s.charAt(left), freq.get(s.charAt(left)) - 1); | ||
if (freq.get(s.charAt(left)) == 0) { | ||
freq.remove(s.charAt(left)); | ||
} | ||
left++; | ||
} | ||
} | ||
|
||
return cnt; | ||
} | ||
} |
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.
Uh where's the code breakdown