Subarry | ||
---|---|---|
Sum | Prefix Sum | |
Min | Mono Stack | |
See Problem 560, very useful.
Function | Method | |
---|---|---|
Reverse a vector | std::reverse | |
First element less than provided | std::upper_bound | |
First element greater than provided | std::lower_bound | |
Find string/characterin string | std::string.find | |
- The value in the result only changes at range boundaries
- The left boundary will contribute +inc
- The right boundary will contribute -inc
- Scan the ranges and calculate the contribution
- Scan the array and calculate the running sum
- Time complexity:
$O(M+N)$ - Space complexity:
$O(N)$
Simple prefix XOR;
- 2D prefix sum & subarray sum to target problem
- Time complexity
$O(M^2N)$
- Negative flowers affect the total beauty
- Iterate over valid pairs
- Similar to 560. Subarray Sum Equals K
- Time complexity:
$O(n)$ - Space complexity:
$O(1)$
class Solution {
public:
long long numberOfSubstrings(string s) {
int n = s.size();
long long ret = 0;
long long hash[26] = {0};
for(int i = 0;i < n;++i){
ret += hash[s[i] - 'a'];
hash[s[i] - 'a']++;
}
return ret + n;
}
};