Input: "aaabc"Output: 2Explanation:
We can make `s` good by applying these operations:* Change one occurrence of `'a'` to `'b'`* Insert one occurrence of `'c'` into `s`
To make all character frequencies equal, we can enumerate all possible target frequencies and number of distinct characters, and for each, calculate the minimum operations needed to reach that configuration using insert, delete, and change operations.
classSolution {
public:int minOperations(string s) {
vector<int> freq(26, 0);
for (char c : s) freq[c -'a']++;
int ans = INT_MAX, n = s.size();
for (int chars =1; chars <=26; ++chars) {
for (int f =1; f * chars <= n; ++f) {
vector<int> v = freq;
sort(v.rbegin(), v.rend());
int cost =0;
for (int i =0; i < chars; ++i) cost += abs(v[i] - f);
for (int i = chars; i <26; ++i) cost += v[i];
ans = min(ans, cost);
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicintminOperations(String s) {
int[] freq =newint[26];
for (char c : s.toCharArray()) freq[c -'a']++;
int ans = Integer.MAX_VALUE, n = s.length();
for (int chars = 1; chars <= 26; chars++) {
for (int f = 1; f * chars <= n; f++) {
int[] v = freq.clone();
Arrays.sort(v);
int cost = 0;
for (int i = 0; i < chars; i++) cost += Math.abs(v[25-i]- f);
for (int i = chars; i < 26; i++) cost += v[25-i];
ans = Math.min(ans, cost);
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from collections import Counter
classSolution:
defminOperations(self, s: str) -> int:
freq = [0]*26for c in s:
freq[ord(c)-97] +=1 n = len(s)
ans = float('inf')
for chars in range(1, 27):
for f in range(1, n//chars+1):
v = sorted(freq, reverse=True)
cost = sum(abs(v[i]-f) for i in range(chars)) + sum(v[i] for i in range(chars, 26))
ans = min(ans, cost)
return ans
classSolution {
funminOperations(s: String): Int {
val freq = IntArray(26)
for (c in s) freq[c - 'a']++val n = s.length
var ans = Int.MAX_VALUE
for (chars in1..26) {
for (f in1..(n/chars)) {
val v = freq.sortedDescending()
var cost = 0for (i in0 until chars) cost += kotlin.math.abs(v[i] - f)
for (i in chars until 26) cost += v[i]
ans = minOf(ans, cost)
}
}
return ans
}
}
impl Solution {
pubfnmin_operations(s: String) -> i32 {
letmut freq =vec![0; 26];
for c in s.chars() {
freq[c asusize-'a'asusize] +=1;
}
let n = s.len();
letmut ans =i32::MAX;
for chars in1..=26 {
for f in1..=n/chars {
letmut v = freq.clone();
v.sort_by(|a, b| b.cmp(a));
letmut cost =0;
for i in0..chars {
cost += (v[i] asi32- f asi32).abs();
}
for i in chars..26 {
cost += v[i] asi32;
}
if cost < ans { ans = cost; }
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
minOperations(s: string):number {
constfreq= Array(26).fill(0);
for (constcofs) freq[c.charCodeAt(0) -97]++;
constn=s.length;
letans= Number.MAX_SAFE_INTEGER;
for (letchars=1; chars<=26; chars++) {
for (letf=1; f*chars<=n; f++) {
constv= [...freq].sort((a, b) =>b-a);
letcost=0;
for (leti=0; i<chars; i++) cost+= Math.abs(v[i] -f);
for (leti=chars; i<26; i++) cost+=v[i];
ans= Math.min(ans, cost);
}
}
returnans;
}
}