-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeMinimumCharacterstoSatisfyOneofThreeConditions.cpp
More file actions
44 lines (42 loc) · 1.28 KB
/
changeMinimumCharacterstoSatisfyOneofThreeConditions.cpp
File metadata and controls
44 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Source: https://leetcode.com/problems/change-minimum-characters-to-satisfy-one-of-three-conditions/
// Author: Miao Zhang
// Date: 2021-06-02
class Solution {
public:
int minCharacters(string a, string b) {
vector<int> cnta(26);
vector<int> cntb(26);
for (char& c: a) cnta[c - 'a']++;
for (char& c: b) cntb[c - 'a']++;
int res = INT_MAX;
for (int c = 0; c < 26; c++) {
if (c > 0) {
int change = 0;
for (int i = c; i < 26; i++) {
change += cnta[i];
}
for (int i = 0; i < c; i++) {
change += cntb[i];
}
res = min(res, change);
change = 0;
for (int i = c; i < 26; i++) {
change += cntb[i];
}
for (int i = 0; i < c; i++) {
change += cnta[i];
}
res = min(res, change);
}
int change = 0;
for (int i = 0; i < 26; i++) {
if (i != c) {
change += cnta[i];
change += cntb[i];
}
}
res = min(res, change);
}
return res;
}
};