C++ Easy Binary search
\nclass Solution {\npublic:\n    int numberOfSubstrings(string s) {\n        int n=s.size(),ans=0;\n        vector<int>a,b,c;\n        for(int j=0;j<n;j++){\n  
```\nclass Solution {\npublic:\n    int numberOfSubstrings(string s) {\n        int n=s.size(),ans=0;\n        vector<int>a,b,c;\n        for(int j=0;j<n;j++){\n            if(s[j]==\'a\') a.push_back(j);\n            else if(s[j]==\'b\') b.push_back(j);\n            else c.push_back(j);\n        }\n        for(int j=n-3;j>=0;j--){\n            if(s[j]==\'a\'){\n                auto it1=lower_bound(b.begin(),b.end(),j),it2=lower_bound(c.begin(),c.end(),j);\n                if(it1!=b.end() && it2!=c.end()) ans+=(n-max(*it1,*it2));\n            }\n            if(s[j]==\'b\'){\n                auto it1=lower_bound(c.begin(),c.end(),j),it2=lower_bound(a.begin(),a.end(),j);\n                if(it1!=c.end() && it2!=a.end()) ans+=(n-max(*it1,*it2));\n            }\n            if(s[j]==\'c\'){\n                auto it1=lower_bound(a.begin(),a.end(),j),it2=lower_bound(b.begin(),b.end(),j);\n                if(it1!=a.end() && it2!=b.end()) ans+=(n-max(*it1,*it2));\n            }\n        }\n        return ans;\n    }\n};\n```
[]