Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Comment on lines +6 to +7
Copy link
Member

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

### 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
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}