|
51 | 51 |
|
52 | 52 | <!-- 这里可写通用的实现逻辑 -->
|
53 | 53 |
|
54 |
| -前缀和 |
| 54 | +**方法一:计数 + 枚举** |
| 55 | + |
| 56 | +我们先统计字符串 $a$ 和 $b$ 中每个字母出现的次数,记为 $cnt_1$ 和 $cnt_2$。 |
| 57 | + |
| 58 | +然后考虑条件 $3$,即 $a$ 和 $b$ 中的每个字母都相同。我们只需要枚举最终的字母 $c$,然后统计 $a$ 和 $b$ 中不是 $c$ 的字母的个数,即为需要改变的字符个数。 |
| 59 | + |
| 60 | +然后考虑条件 $1$ 和 $2$,即 $a$ 中的每个字母都小于 $b$ 中的每个字母,或者 $b$ 中的每个字母都小于 $a$ 中的每个字母。对于条件 $1$,我们令字符串 $a$ 所有字符都小于字符 $c$,字符串 $b$ 所有字符都不小于 $c$,枚举 $c$ 找出最小的答案即可。条件 $2$ 同理。 |
| 61 | + |
| 62 | +最终的答案即为上述三种情况的最小值。 |
| 63 | + |
| 64 | +时间复杂度 $O(m + n + C^2)$,其中 $m$ 和 $n$ 分别为字符串 $a$ 和 $b$ 的长度,而 $C$ 为字符集大小。本题中 $C = 26$。 |
55 | 65 |
|
56 | 66 | <!-- tabs:start -->
|
57 | 67 |
|
|
60 | 70 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 71 |
|
62 | 72 | ```python
|
63 |
| - |
| 73 | +class Solution: |
| 74 | + def minCharacters(self, a: str, b: str) -> int: |
| 75 | + def f(cnt1, cnt2): |
| 76 | + for i in range(1, 26): |
| 77 | + t = sum(cnt1[i:]) + sum(cnt2[:i]) |
| 78 | + nonlocal ans |
| 79 | + ans = min(ans, t) |
| 80 | + |
| 81 | + m, n = len(a), len(b) |
| 82 | + cnt1 = [0] * 26 |
| 83 | + cnt2 = [0] * 26 |
| 84 | + for c in a: |
| 85 | + cnt1[ord(c) - ord('a')] += 1 |
| 86 | + for c in b: |
| 87 | + cnt2[ord(c) - ord('a')] += 1 |
| 88 | + ans = m + n |
| 89 | + for c1, c2 in zip(cnt1, cnt2): |
| 90 | + ans = min(ans, m + n - c1 - c2) |
| 91 | + f(cnt1, cnt2) |
| 92 | + f(cnt2, cnt1) |
| 93 | + return ans |
64 | 94 | ```
|
65 | 95 |
|
66 | 96 | ### **Java**
|
67 | 97 |
|
68 | 98 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 99 |
|
70 | 100 | ```java
|
| 101 | +class Solution { |
| 102 | + private int ans; |
| 103 | + |
| 104 | + public int minCharacters(String a, String b) { |
| 105 | + int m = a.length(), n = b.length(); |
| 106 | + int[] cnt1 = new int[26]; |
| 107 | + int[] cnt2 = new int[26]; |
| 108 | + for (int i = 0; i < m; ++i) { |
| 109 | + ++cnt1[a.charAt(i) - 'a']; |
| 110 | + } |
| 111 | + for (int i = 0; i < n; ++i) { |
| 112 | + ++cnt2[b.charAt(i) - 'a']; |
| 113 | + } |
| 114 | + ans = m + n; |
| 115 | + for (int i = 0; i < 26; ++i) { |
| 116 | + ans = Math.min(ans, m + n - cnt1[i] - cnt2[i]); |
| 117 | + } |
| 118 | + f(cnt1, cnt2); |
| 119 | + f(cnt2, cnt1); |
| 120 | + return ans; |
| 121 | + } |
71 | 122 |
|
| 123 | + private void f(int[] cnt1, int[] cnt2) { |
| 124 | + for (int i = 1; i < 26; ++i) { |
| 125 | + int t = 0; |
| 126 | + for (int j = i; j < 26; ++j) { |
| 127 | + t += cnt1[j]; |
| 128 | + } |
| 129 | + for (int j = 0; j < i; ++j) { |
| 130 | + t += cnt2[j]; |
| 131 | + } |
| 132 | + ans = Math.min(ans, t); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### **C++** |
| 139 | + |
| 140 | +```cpp |
| 141 | +class Solution { |
| 142 | +public: |
| 143 | + int minCharacters(string a, string b) { |
| 144 | + int m = a.size(), n = b.size(); |
| 145 | + vector<int> cnt1(26); |
| 146 | + vector<int> cnt2(26); |
| 147 | + for (char& c : a) ++cnt1[c - 'a']; |
| 148 | + for (char& c : b) ++cnt2[c - 'a']; |
| 149 | + int ans = m + n; |
| 150 | + for (int i = 0; i < 26; ++i) ans = min(ans, m + n - cnt1[i] - cnt2[i]); |
| 151 | + auto f = [&](vector<int>& cnt1, vector<int>& cnt2) { |
| 152 | + for (int i = 1; i < 26; ++i) { |
| 153 | + int t = 0; |
| 154 | + for (int j = i; j < 26; ++j) t += cnt1[j]; |
| 155 | + for (int j = 0; j < i; ++j) t += cnt2[j]; |
| 156 | + ans = min(ans, t); |
| 157 | + } |
| 158 | + }; |
| 159 | + f(cnt1, cnt2); |
| 160 | + f(cnt2, cnt1); |
| 161 | + return ans; |
| 162 | + } |
| 163 | +}; |
| 164 | +``` |
| 165 | +
|
| 166 | +### **Go** |
| 167 | +
|
| 168 | +```go |
| 169 | +func minCharacters(a string, b string) int { |
| 170 | + cnt1 := [26]int{} |
| 171 | + cnt2 := [26]int{} |
| 172 | + for _, c := range a { |
| 173 | + cnt1[c-'a']++ |
| 174 | + } |
| 175 | + for _, c := range b { |
| 176 | + cnt2[c-'a']++ |
| 177 | + } |
| 178 | + m, n := len(a), len(b) |
| 179 | + ans := m + n |
| 180 | + for i := 0; i < 26; i++ { |
| 181 | + ans = min(ans, m+n-cnt1[i]-cnt2[i]) |
| 182 | + } |
| 183 | + f := func(cnt1, cnt2 [26]int) { |
| 184 | + for i := 1; i < 26; i++ { |
| 185 | + t := 0 |
| 186 | + for j := i; j < 26; j++ { |
| 187 | + t += cnt1[j] |
| 188 | + } |
| 189 | + for j := 0; j < i; j++ { |
| 190 | + t += cnt2[j] |
| 191 | + } |
| 192 | + ans = min(ans, t) |
| 193 | + } |
| 194 | + } |
| 195 | + f(cnt1, cnt2) |
| 196 | + f(cnt2, cnt1) |
| 197 | + return ans |
| 198 | +} |
| 199 | +
|
| 200 | +func min(a, b int) int { |
| 201 | + if a < b { |
| 202 | + return a |
| 203 | + } |
| 204 | + return b |
| 205 | +} |
72 | 206 | ```
|
73 | 207 |
|
74 | 208 | ### **TypeScript**
|
|
0 commit comments