1358. 包含所有三种字符的子字符串数目
题面:
题解:
双指针找区间即可。
代码:
class Solution { public: int numberOfSubstrings(string s) { int numa = 0,numb = 0,numc = 0,l = 0,r = 0; int n = s.size(); int ans=0; if(s[0] == 'a')numa++; if(s[0] == 'b')numb++; if(s[0] == 'c')numc++; while(r<n) { while(r+1<n&&!(numa&&numb&&numc)) { if(s[r+1] == 'a')numa++; if(s[r+1] == 'b')numb++; if(s[r+1] == 'c')numc++; r++; } if(numa&&numb&&numc) { // cout<<l<<" "<<r<<endl; ans += n-r; } else { break; } if(s[l] == 'a')numa--; if(s[l] == 'b')numb--; if(s[l] == 'c')numc--; l++; } return ans; } };