26. Game with String
β GFG solution to the Game with String problem: minimize string value after removing k characters using greedy strategies. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a string s
consisting of lowercase alphabets and an integer k
. You must remove exactly k characters from the string such that the value of the string is minimized.
The value of the string is defined as the sum of squares of the frequencies of all distinct characters.
Return the minimum value achievable after removing exactly k
characters.
π Examples
Example 1
Input: s = "abbccc", k = 2
Output: 6
Explanation: Remove two 'c' to get frequencies: a=1, b=2, c=1 β 1Β² + 2Β² + 1Β² = 6
Example 2
Input: s = "aaab", k = 2
Output: 2
Explanation: Remove two 'a' to get: a=1, b=1 β 1Β² + 1Β² = 2
π Constraints
$0 \leq k \leq \text{s.length()} \leq 10^5$
$s$ consists of lowercase English letters only
β
My Approach
Frequency Bucket Reduction + Greedy
We aim to reduce the frequencies of the most common characters first, because squaring makes high frequencies dominate the total value. So, a greedy strategy works best.
π‘ Algorithm Steps:
Count frequencies of each character using an array
f[26]
.Find the maximum frequency
m
among characters.Create a bucket array
b[m+1]
whereb[i]
stores how many characters have frequencyi
.Starting from the largest frequency:
If we can remove all
b[i]
characters with frequencyi
, move them to frequencyi-1
.Repeat until
k
becomes zero.
Finally, compute the total value: for all frequencies
i
, addb[i] * i * i
to result.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n + k), where n = size of input string, and frequency reduction takes at most O(k) operations.
Expected Auxiliary Space Complexity: O(n), used for frequency and bucket arrays of size up to max frequency.
π§βπ» Code (C++)
class Solution {
public:
int minValue(string &s, int k) {
int f[26] = {}, m = 0;
for (char c : s) m = max(m, ++f[c - 'a']);
vector<int> b(m + 1);
for (int x : f) if (x) b[x]++;
while (k && m) {
if (b[m] <= k) k -= b[m], b[m - 1] += b[m], b[m--] = 0;
else b[m] -= k, b[m - 1] += k, k = 0;
}
int r = 0;
for (int i = 1; i <= m; i++) r += i * i * b[i];
return r;
}
};
π§βπ» Code (Java)
class Solution {
public int minValue(String s, int k) {
int[] f = new int[26]; int m = 0;
for (char c : s.toCharArray()) m = Math.max(m, ++f[c - 'a']);
int[] b = new int[m + 1];
for (int x : f) if (x > 0) b[x]++;
while (k > 0 && m > 0) {
if (b[m] <= k) {
k -= b[m];
b[m - 1] += b[m];
b[m--] = 0;
} else {
b[m] -= k;
b[m - 1] += k;
k = 0;
}
}
int r = 0;
for (int i = 1; i <= m; i++) r += i * i * b[i];
return r;
}
}
π Code (Python)
class Solution:
def minValue(self, s, k):
f = [0] * 26
for c in s: f[ord(c) - 97] += 1
m = max(f)
b = [0] * (m + 1)
for x in f: b[x] += x > 0
while k and m:
if b[m] <= k: k -= b[m]; b[m - 1] += b[m]; b[m] = 0; m -= 1
else: b[m] -= k; b[m - 1] += k; k = 0
return sum(i * i * b[i] for i in range(1, m + 1))
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated