class Solution { public: long long int cal(string s,int k){ int n = s.size(); int freq[26] = {0}; int dist_cnt = 0; long long int ans = 0; //ans count int i=0; //start of window int j=0; //end of window while(j<n) { freq[s[j]-'a']++; if(freq[s[j]-'a'] == 1) dist_cnt++; //Decreasing the size of window while(dist_cnt > k) { freq[s[i]-'a']--; if(freq[s[i]-'a'] == 0) dist_cnt--; i++; } j++; ans += (j-i+1); } return ans; } long long int substrCount (string s, int k) { long long ans=cal(s,k)-cal(s,k-1); return ans; } };
long long int substrCount (string s, int k) { return cal(s,k)-cal(s,k-1); // it's a substraction, providing the final difference value for a fixed 'k'. <- it's "answer" // try 'cal(s,k)' and try cal(s,k-1) alone by return. // it's math applying to get the number of answer ( a gap between k and k-1 ) // }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)