easy peesy to make you water.
Intuition\n Describe your first thoughts on how to solve this problem. \n# GUYS IF U WANT JOB THEN UPVOTE. XD !! JUST KIDDING FRIENDS GO ON AND READ THE POST. I
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n# GUYS IF U WANT JOB THEN UPVOTE. XD !! JUST KIDDING FRIENDS GO ON AND READ THE POST. I LOVE U PPL.\n\n**Key Points:**\n\n- The code uses a sliding window with two pointers (`i` and `j`) and a HashMap to efficiently track distinct characters and their frequencies within the window.\n- It checks for the condition of having exactly three distinct characters and calculates the number of valid substrings based on the window size and remaining string length.\n- The window is contracted from the left by removing characters and updating their frequencies in the HashMap.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n1. **Initializing Variables:**\n   - `int i = 0`: Initializes `i` as the starting index of the current substring window.\n   - `int j = 0`: Initializes `j` as the ending index of the current substring window (starts from 0).\n   - `int ans = 0`: Initializes `ans` to store the count of substrings found so far (initially 0).\n   - `int n = s.length()`: Stores the length of the input string `s` in the variable `n`.\n   - `HashMap<Character, Integer> hm = new HashMap<>()`: Creates a HashMap `hm` to keep track of the characters encountered in the current window and their frequencies.\n\n2. **Sliding Window Technique:**\n   - The `while` loop iterates as long as `i` is less than or equal to `n - 3` (to ensure the window can at most contain three characters). This implements a sliding window approach to explore all possible substrings.\n     - The inner `while` loop iterates as long as `j` is less than `n` and the number of distinct characters in the window (`hm.size()`) is less than 3.\n       - `hm.put(s.charAt(j), hm.getOrDefault(s.charAt(j), 0) + 1)`: Adds the current character (`s.charAt(j)`) to the `hm` HashMap. It uses `getOrDefault` to get the existing count (0 if not present) and increments it. This keeps track of the frequency of each character in the window.\n       - `j++`: Increments `j` to move the window one step to the right, potentially adding new characters.\n\n3. **Counting Substrings:**\n   - `if (hm.size() == 3)`: Checks if the current window has exactly three distinct characters.\n     - `ans += n - j + 1`: If there are three distinct characters, all substrings starting from the current `i` (inclusive) to positions before `j` (exclusive) are valid because the window size can\'t exceed `n - i`. This calculates the number of valid substrings starting from `i` and adds it to `ans`.\n\n4. **Sliding Window Contraction:**\n   - `hm.put(s.charAt(i), hm.get(s.charAt(i)) - 1)`: Decrements the frequency of the character at the beginning of the window (`s.charAt(i)`) in the `hm` HashMap. This reflects the removal of that character from the window.\n   - `if (hm.get(s.charAt(i)) == 0)`: Checks if the frequency of the character at the beginning of the window becomes zero.\n     - `hm.remove(s.charAt(i))`: If the frequency becomes zero, it means the character is no longer present in the window, so it\'s removed from the `hm` HashMap.\n   - `i++`: Increments `i` to move the window one step to the right, effectively removing the character at the beginning from the current window.\n\n5. **Returning the Result:**\n   - `return ans`: After iterating through all possible windows, `ans` holds the total count of substrings with at most three distinct characters. The code returns this value.\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThis approach provides a time-efficient solution (O(n)) for finding the desired substrings.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution {\n    public int numberOfSubstrings(String s) {\n        int i=0;\n        int j=0;\n        int ans=0;\n        int n=s.length();\n        HashMap<Character,Integer> hm=new HashMap<>();\n\n        while(i<=n-3){\n            while(j<n && hm.size()<3){\n                hm.put(s.charAt(j),hm.getOrDefault(s.charAt(j),0)+1);\n                j++;\n            }\n            \n            if(hm.size()==3){\n                ans+=n-j+1;\n            }\n            \n            hm.put(s.charAt(i),hm.get(s.charAt(i))-1);\n            if(hm.get(s.charAt(i))==0){\n                hm.remove(s.charAt(i));\n            }\n            i++;\n        }\n        return ans;\n    }\n}\n```
['Hash Table', 'String', 'Sliding Window', 'Java']