712102. Sum of Beauty of All Substrings
Here is a detailed Markdown blog post for the GeeksforGeeks problem "Sum of Beauty of All Substrings":
712102: Sum of Beauty of All Substrings
Summary
Given a string s
, we need to find the sum of beauty of all substrings. The beauty of a substring is the product of its length and the frequency of each character in that substring.
Detailed Explanation
To solve this problem, we can start by considering the definition of beauty for a given substring. The beauty is calculated as the product of the length of the substring and the frequency of each unique character in the substring.
We can use a dictionary to store the frequency of characters in the current window (substring). Initialize an empty dictionary freq
to store the frequency of characters. Then, iterate over the string s
. For each character c
, increment its count in the freq
dictionary.
Next, calculate the beauty for each substring by multiplying the length of the substring with the sum of frequencies of all unique characters in that substring. To achieve this, we can use a variable sum_freq
to keep track of the sum of frequencies and then multiply it with the length of the current substring.
Finally, add up the beauty values for all substrings using a variable total_beauty
. This will give us the total sum of beauty for all substrings.
Here is a step-by-step breakdown:
- Initialize an empty dictionary
freq
to store the frequency of characters. - Iterate over the string
s
. - For each character
c
, increment its count in thefreq
dictionary. - Calculate the beauty for each substring by multiplying the length of the substring with the sum of frequencies of all unique characters in that substring.
- Add up the beauty values for all substrings using a variable
total_beauty
.
Time complexity: O(n), where n is the length of the string s
. We are iterating over the string once.
Space complexity: O(min(n, 26)), as we need to store the frequency of characters in the dictionary. If the frequency of any character exceeds the size of the alphabet (26), we can consider increasing the space complexity to O(n).
Optimized Solutions
Java
public class Main {
public static void main(String[] args) {
String s = "abc";
int totalBeauty = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = i; j < s.length(); j++) {
String substring = s.substring(i, j + 1);
int beauty = substring.length() * getFrequency(substring).stream().mapToInt(Integer::intValue).sum();
totalBeauty += beauty;
}
}
System.out.println(totalBeauty);
}
public static HashMap<Character, Integer> getFrequency(String str) {
HashMap<Character, Integer> freqMap = new HashMap<>();
for (char c : str.toCharArray()) {
freqMap.put(c, freqMap.getOrDefault(c, 0) + 1);
}
return freqMap;
}
}
Code Editor (Testing phase)
Improve Your Solution
Use the editor below to refine the provided solution. Select a programming language and try the following:
- Add import statement if required.
- Optimize the code for better time or space complexity.
- Add test cases to validate edge cases and common scenarios.
- Handle error conditions or invalid inputs gracefully.
- Experiment with alternative approaches to deepen your understanding.
Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.