|
| 1 | +--- |
| 2 | +id: sum-of-beauty-of-all-substrings |
| 3 | +title: 1781. Sum of Beauty of All Substrings |
| 4 | +sidebar_label: 1781. Sum of Beauty of All Substrings |
| 5 | +tags: |
| 6 | +- Array |
| 7 | +- Hash Table |
| 8 | +- Counting |
| 9 | + |
| 10 | +description: "This is a solution to the Sum of Beauty of All Substrings problem on LeetCode." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | +The beauty of a string is the difference in frequencies between the most frequent and least frequent characters. |
| 15 | + |
| 16 | +For example, the beauty of "abaacc" is 3 - 1 = 2. |
| 17 | +Given a string s, return the sum of beauty of all of its substrings. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | +``` |
| 23 | +Input: s = "aabcb" |
| 24 | +Output: 5 |
| 25 | +Explanation: The substrings with non-zero beauty are ["aab","aabc","aabcb","abcb","bcb"], each with beauty equal to 1. |
| 26 | +``` |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +``` |
| 31 | +Input: s = "aabcbaa" |
| 32 | +Output: 17 |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +### Constraints |
| 37 | +- `1 <= s.length <= 500` |
| 38 | +- `s consists of only lowercase English letters.` |
| 39 | +## Solution for 1781. Sum of Beauty of All Substrings Problem |
| 40 | +### Approach |
| 41 | +The goal of the `beautySum` problem is to find the sum of beauty values for all possible substrings of a given string `s`. The beauty value of a substring is defined as the difference between the maximum frequency and the minimum frequency of characters within that substring. |
| 42 | + |
| 43 | +#### Steps to Solve the Problem |
| 44 | + |
| 45 | +1. **Initialization**: |
| 46 | + - Initialize a variable `ans` to store the sum of beauty values. |
| 47 | + |
| 48 | +2. **Iterate through the string**: |
| 49 | + - Use a nested loop where the outer loop (`i`) iterates through each character in the string and the inner loop (`j`) iterates from the current character to the end of the string, effectively generating all substrings starting from each character. |
| 50 | + |
| 51 | +3. **Track character frequencies**: |
| 52 | + - Use a dictionary (or hashmap) to keep track of the frequency of each character in the current substring. |
| 53 | + - Update the dictionary as you extend the substring in the inner loop. |
| 54 | + |
| 55 | +4. **Calculate the beauty value**: |
| 56 | + - For each substring, calculate its beauty value by finding the maximum and minimum frequencies of characters in the substring. |
| 57 | + - Update the sum of beauty values (`ans`) with the beauty value of the current substring. |
| 58 | + |
| 59 | +5. **Return the result**: |
| 60 | + - After processing all substrings, return the total sum of beauty values. |
| 61 | +<Tabs> |
| 62 | + <TabItem value="Solution" label="Solution"> |
| 63 | + |
| 64 | + #### Implementation |
| 65 | + ```jsx live |
| 66 | + function Solution(arr) { |
| 67 | + function beauty(mp) { |
| 68 | + let mini = Infinity; |
| 69 | + let maxi = -Infinity; |
| 70 | + |
| 71 | + for (let key in mp) { |
| 72 | + maxi = Math.max(maxi, mp[key]); |
| 73 | + mini = Math.min(mini, mp[key]); |
| 74 | + } |
| 75 | + |
| 76 | + return maxi - mini; |
| 77 | + } |
| 78 | + |
| 79 | + function beautySum(s) { |
| 80 | + let ans = 0; |
| 81 | + for (let i = 0; i < s.length; i++) { |
| 82 | + let mp = {}; |
| 83 | + |
| 84 | + for (let j = i; j < s.length; j++) { |
| 85 | + mp[s[j]] = (mp[s[j]] || 0) + 1; |
| 86 | + ans += beauty(mp); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return ans; |
| 91 | + } |
| 92 | + const input = "aabcb" |
| 93 | + const output = beautySum(input) |
| 94 | + return ( |
| 95 | + <div> |
| 96 | + <p> |
| 97 | + <b>Input: </b> |
| 98 | + {JSON.stringify(input)} |
| 99 | + </p> |
| 100 | + <p> |
| 101 | + <b>Output:</b> {output.toString()} |
| 102 | + </p> |
| 103 | + </div> |
| 104 | + ); |
| 105 | + } |
| 106 | + ``` |
| 107 | + |
| 108 | + #### Complexity Analysis |
| 109 | + |
| 110 | + - Time Complexity: $ O(N^2) $ |
| 111 | + - Space Complexity: $ O(N)$ |
| 112 | + |
| 113 | + ## Code in Different Languages |
| 114 | + <Tabs> |
| 115 | + <TabItem value="JavaScript" label="JavaScript"> |
| 116 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 117 | + ```javascript |
| 118 | + function beauty(mp) { |
| 119 | + let mini = Infinity; |
| 120 | + let maxi = -Infinity; |
| 121 | + |
| 122 | + for (let key in mp) { |
| 123 | + maxi = Math.max(maxi, mp[key]); |
| 124 | + mini = Math.min(mini, mp[key]); |
| 125 | + } |
| 126 | + |
| 127 | + return maxi - mini; |
| 128 | + } |
| 129 | + |
| 130 | + function beautySum(s) { |
| 131 | + let ans = 0; |
| 132 | + for (let i = 0; i < s.length; i++) { |
| 133 | + let mp = {}; |
| 134 | + |
| 135 | + for (let j = i; j < s.length; j++) { |
| 136 | + mp[s[j]] = (mp[s[j]] || 0) + 1; |
| 137 | + ans += this.beauty(mp); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return ans; |
| 142 | + } |
| 143 | + ``` |
| 144 | + |
| 145 | + </TabItem> |
| 146 | + <TabItem value="TypeScript" label="TypeScript"> |
| 147 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 148 | + ```typescript |
| 149 | + class Solution { |
| 150 | + beauty(mp: { [key: string]: number }): number { |
| 151 | + let mini = Infinity; |
| 152 | + let maxi = -Infinity; |
| 153 | +
|
| 154 | + for (let key in mp) { |
| 155 | + maxi = Math.max(maxi, mp[key]); |
| 156 | + mini = Math.min(mini, mp[key]); |
| 157 | + } |
| 158 | +
|
| 159 | + return maxi - mini; |
| 160 | + } |
| 161 | +
|
| 162 | + beautySum(s: string): number { |
| 163 | + let ans = 0; |
| 164 | + for (let i = 0; i < s.length; i++) { |
| 165 | + let mp: { [key: string]: number } = {}; |
| 166 | +
|
| 167 | + for (let j = i; j < s.length; j++) { |
| 168 | + mp[s[j]] = (mp[s[j]] || 0) + 1; |
| 169 | + ans += this.beauty(mp); |
| 170 | + } |
| 171 | + } |
| 172 | +
|
| 173 | + return ans; |
| 174 | + } |
| 175 | +} |
| 176 | +
|
| 177 | + ``` |
| 178 | + </TabItem> |
| 179 | + <TabItem value="Python" label="Python"> |
| 180 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 181 | + ```python |
| 182 | + class Solution: |
| 183 | + def beauty(self, mp): |
| 184 | + mini = float('inf') |
| 185 | + maxi = float('-inf') |
| 186 | +
|
| 187 | + for value in mp.values(): |
| 188 | + maxi = max(maxi, value) |
| 189 | + mini = min(mini, value) |
| 190 | +
|
| 191 | + return maxi - mini |
| 192 | +
|
| 193 | + def beautySum(self, s: str) -> int: |
| 194 | + ans = 0 |
| 195 | + for i in range(len(s)): |
| 196 | + mp = {} |
| 197 | +
|
| 198 | + for j in range(i, len(s)): |
| 199 | + mp[s[j]] = mp.get(s[j], 0) + 1 |
| 200 | + ans += self.beauty(mp) |
| 201 | +
|
| 202 | + return ans |
| 203 | +
|
| 204 | + ``` |
| 205 | + |
| 206 | + </TabItem> |
| 207 | + <TabItem value="Java" label="Java"> |
| 208 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 209 | + ```java |
| 210 | + import java.util.HashMap; |
| 211 | +
|
| 212 | +class Solution { |
| 213 | + private int beauty(HashMap<Character, Integer> mp) { |
| 214 | + int mini = Integer.MAX_VALUE; |
| 215 | + int maxi = Integer.MIN_VALUE; |
| 216 | +
|
| 217 | + for (int value : mp.values()) { |
| 218 | + maxi = Math.max(maxi, value); |
| 219 | + mini = Math.min(mini, value); |
| 220 | + } |
| 221 | +
|
| 222 | + return maxi - mini; |
| 223 | + } |
| 224 | +
|
| 225 | + public int beautySum(String s) { |
| 226 | + int ans = 0; |
| 227 | + for (int i = 0; i < s.length(); i++) { |
| 228 | + HashMap<Character, Integer> mp = new HashMap<>(); |
| 229 | +
|
| 230 | + for (int j = i; j < s.length(); j++) { |
| 231 | + mp.put(s.charAt(j), mp.getOrDefault(s.charAt(j), 0) + 1); |
| 232 | + ans += beauty(mp); |
| 233 | + } |
| 234 | + } |
| 235 | +
|
| 236 | + return ans; |
| 237 | + } |
| 238 | +} |
| 239 | +
|
| 240 | + ``` |
| 241 | + |
| 242 | + </TabItem> |
| 243 | + <TabItem value="C++" label="C++"> |
| 244 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 245 | + ```cpp |
| 246 | + class Solution { |
| 247 | + int beauty(unordered_map<char,int>&mp) |
| 248 | + { |
| 249 | + int mini=INT_MAX; |
| 250 | + int maxi=INT_MIN; |
| 251 | +
|
| 252 | + for(auto i:mp) |
| 253 | + { |
| 254 | + maxi=max(maxi,i.second); |
| 255 | + mini=min(mini,i.second); |
| 256 | + } |
| 257 | +
|
| 258 | + return maxi-mini; |
| 259 | +
|
| 260 | + } |
| 261 | +public: |
| 262 | + int beautySum(string s) { |
| 263 | + int ans=0; |
| 264 | + for(int i=0;i<s.size();i++) |
| 265 | + { |
| 266 | + unordered_map<char,int>mp; |
| 267 | +
|
| 268 | + for(int j=i;j<s.size();j++) |
| 269 | + { |
| 270 | + mp[s[j]]++; |
| 271 | + ans+=beauty(mp); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + return ans; |
| 276 | + } |
| 277 | +}; |
| 278 | + ``` |
| 279 | +</TabItem> |
| 280 | +</Tabs> |
| 281 | + |
| 282 | + </TabItem> |
| 283 | +</Tabs> |
| 284 | + |
| 285 | +## References |
| 286 | + |
| 287 | +- **LeetCode Problem**: [ Sum of Beauty of All Substrings](https://leetcode.com/problems/sum-of-beauty-of-all-substrings/description/) |
| 288 | + |
| 289 | +- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/sum-of-beauty-of-all-substrings/description/) |
| 290 | + |
0 commit comments