Simple Java solution O(n), using two pointers (with explanation )
let i - j is the window which contains all the chars\nintially i and j are 0\nkeep on incrementeting j until our window (i to j) contains at least one \'a\', \'
let i - j is the window which contains all the chars\nintially i and j are 0\nkeep on incrementeting j until our window (i to j) contains at least one \'a\', \'b\' and \'c\'\nonce found note that not only i - j but all the substring i - j + 1 , i - j + 2 and i - s.length() - 1 are the required windows..so get its counts\nNow there may be other windows which end at j but start from i + 1 onwards...so increment i and see if we can find our window i.e. if i + 1....j is a window or not ...if it is get its counts otherwise stop incrementing i as we\'ll not find the required string because if i + 1...j is not the required string ..i + 2......j, i + 3......j also will not have the string....in this case try to\nfind the strings by incrementing the j\n\n```\nclass Solution {\n    public int numberOfSubstrings(String s) {\n        int res = 0, i = 0, j = 0, a = 0, b = 0, c = 0;\n        char[] strs = s.toCharArray();\n        while(j < s.length()){\n            if(strs[j] == \'a\'){\n                a++;\n            }else if(strs[j] == \'b\'){\n                b++;\n            }else{\n                c++;\n            }\n            while(a > 0 && b > 0 && c > 0){\n                res += (s.length() - j);      // i....j is the required substring so is i...j+1, i ...j+2, i....s.length()\n                if(strs[i] == \'a\'){         // increment i and try to find if i+1.....j is the required substring if it is not then we\'ll not find the required string by further incrementing the i so breakout and increment j and try to find the required string\n                    a--;\n                }else if(strs[i] == \'b\'){\n                    b--;\n                }else{\n                    c--;\n                }\n                i++;\n            }\n            j++;\n        }\n        return res;\n    }\n}\n```
[]