New Problem Solution - "Sum of Beauty of All Substrings"
This commit is contained in:
parent
6b2e874856
commit
2e7a56ee42
@ -12,6 +12,7 @@ LeetCode
|
||||
|1786|[Number of Restricted Paths From First to Last Node](https://leetcode.com/problems/number-of-restricted-paths-from-first-to-last-node/) | [C++](./algorithms/cpp/numberOfRestrictedPathsFromFirstToLastNode/NumberOfRestrictedPathsFromFirstToLastNode.cpp)|Medium|
|
||||
|1785|[Minimum Elements to Add to Form a Given Sum](https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/) | [C++](./algorithms/cpp/minimumElementsToAddToFormAGivenSum/MinimumElementsToAddToFormAGivenSum.cpp)|Medium|
|
||||
|1784|[Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/) | [C++](./algorithms/cpp/checkIfBinaryStringHasAtMostOneSegmentOfOnes/CheckIfBinaryStringHasAtMostOneSegmentOfOnes.cpp)|Easy|
|
||||
|1781|[Sum of Beauty of All Substrings](https://leetcode.com/problems/sum-of-beauty-of-all-substrings/) | [C++](./algorithms/cpp/sumOfBeautyOfAllSubstrings/SumOfBeautyOfAllSubstrings.cpp)|Medium|
|
||||
|1780|[Check if Number is a Sum of Powers of Three](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/) | [C++](./algorithms/cpp/checkIfNumberIsASumOfPowersOfThree/CheckIfNumberIsASumOfPowersOfThree.cpp)|Medium|
|
||||
|1779|[Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/) | [C++](./algorithms/cpp/findNearestPointThatHasTheSameXOrYCoordinate/FindNearestPointThatHasTheSameXOrYCoordinate.cpp)|Easy|
|
||||
|1763|[Longest Nice Substring](https://leetcode.com/problems/longest-nice-substring/) | [C++](./algorithms/cpp/longestNiceSubstring/LongestNiceSubstring.cpp)|Easy|
|
||||
|
@ -0,0 +1,82 @@
|
||||
// Source : https://leetcode.com/problems/sum-of-beauty-of-all-substrings/
|
||||
// Author : Hao Chen
|
||||
// Date : 2021-03-13
|
||||
|
||||
/*****************************************************************************************************
|
||||
*
|
||||
* The beauty of a string is the difference in frequencies between the most frequent and least
|
||||
* frequent characters.
|
||||
*
|
||||
* For example, the beauty of "abaacc" is 3 - 1 = 2.
|
||||
*
|
||||
* Given a string s, return the sum of beauty of all of its substrings.
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: s = "aabcb"
|
||||
* Output: 5
|
||||
* Explanation: The substrings with non-zero beauty are ["aab","aabc","aabcb","abcb","bcb"], each with
|
||||
* beauty equal to 1.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: s = "aabcbaa"
|
||||
* Output: 17
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= s.length <= 500
|
||||
* s consists of only lowercase English letters.
|
||||
******************************************************************************************************/
|
||||
|
||||
class Solution {
|
||||
private:
|
||||
int beauty(string& s, int start, int end) {
|
||||
int stat[26] = {0};
|
||||
for (int i=start; i<=end; i++){
|
||||
stat[s[i]-'a']++;
|
||||
}
|
||||
int max = INT_MIN, min = INT_MAX;
|
||||
for (auto s: stat) {
|
||||
if (s == 0 ) continue;
|
||||
max = s > max ? s : max;
|
||||
min = s < min ? s : min;
|
||||
}
|
||||
return max - min;
|
||||
}
|
||||
public:
|
||||
int beautySum(string s) {
|
||||
return beautySum02(s);
|
||||
return beautySum01(s);
|
||||
}
|
||||
|
||||
int beautySum01(string& s) {
|
||||
int sum = 0;
|
||||
for (int i=0; i<s.size()-1; i++) {
|
||||
for (int j=i+1; j<s.size(); j++) {
|
||||
sum += beauty(s, i, j);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
//same as beautySum01(), but optimazed slightly
|
||||
int beautySum02(string& s) {
|
||||
int sum = 0;
|
||||
for (int i=0; i<s.size()-1; i++) {
|
||||
int stat[26] = {0};
|
||||
for (int j=i; j<s.size(); j++) {
|
||||
stat[s[j]-'a']++;
|
||||
int max = INT_MIN, min = INT_MAX;
|
||||
for (auto& n: stat) {
|
||||
if (n <= 0 ) continue;
|
||||
max = n > max ? n : max;
|
||||
min = n < min ? n : min;
|
||||
}
|
||||
//cout << s.substr(i, j-i+1) << " --> "<< max << ":" << min << endl;
|
||||
sum += (max - min);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user