|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3442.Maximum%20Difference%20Between%20Even%20and%20Odd%20Frequency%20I/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3442. Maximum Difference Between Even and Odd Frequency I](https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-i) |
| 10 | + |
| 11 | +[中文文档](/solution/3400-3499/3442.Maximum%20Difference%20Between%20Even%20and%20Odd%20Frequency%20I/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given a string <code>s</code> consisting of lowercase English letters. Your task is to find the <strong>maximum</strong> difference between the frequency of <strong>two</strong> characters in the string such that:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>One of the characters has an <strong>even frequency</strong> in the string.</li> |
| 21 | + <li>The other character has an <strong>odd frequency</strong> in the string.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p>Return the <strong>maximum</strong> difference, calculated as the frequency of the character with an <b>odd</b> frequency <strong>minus</strong> the frequency of the character with an <b>even</b> frequency.</p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1:</strong></p> |
| 28 | + |
| 29 | +<div class="example-block"> |
| 30 | +<p><strong>Input:</strong> <span class="example-io">s = "aaaaabbc"</span></p> |
| 31 | + |
| 32 | +<p><strong>Output:</strong> 3</p> |
| 33 | + |
| 34 | +<p><strong>Explanation:</strong></p> |
| 35 | + |
| 36 | +<ul> |
| 37 | + <li>The character <code>'a'</code> has an <strong>odd frequency</strong> of <code><font face="monospace">5</font></code><font face="monospace">,</font> and <code>'b'</code> has an <strong>even frequency</strong> of <code><font face="monospace">2</font></code>.</li> |
| 38 | + <li>The maximum difference is <code>5 - 2 = 3</code>.</li> |
| 39 | +</ul> |
| 40 | +</div> |
| 41 | + |
| 42 | +<p><strong class="example">Example 2:</strong></p> |
| 43 | + |
| 44 | +<div class="example-block"> |
| 45 | +<p><strong>Input:</strong> <span class="example-io">s = "abcabcab"</span></p> |
| 46 | + |
| 47 | +<p><strong>Output:</strong> 1</p> |
| 48 | + |
| 49 | +<p><strong>Explanation:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li>The character <code>'a'</code> has an <strong>odd frequency</strong> of <code><font face="monospace">3</font></code><font face="monospace">,</font> and <code>'c'</code> has an <strong>even frequency</strong> of <font face="monospace">2</font>.</li> |
| 53 | + <li>The maximum difference is <code>3 - 2 = 1</code>.</li> |
| 54 | +</ul> |
| 55 | +</div> |
| 56 | + |
| 57 | +<p> </p> |
| 58 | +<p><strong>Constraints:</strong></p> |
| 59 | + |
| 60 | +<ul> |
| 61 | + <li><code>3 <= s.length <= 100</code></li> |
| 62 | + <li><code>s</code> consists only of lowercase English letters.</li> |
| 63 | + <li><code>s</code> contains at least one character with an odd frequency and one with an even frequency.</li> |
| 64 | +</ul> |
| 65 | + |
| 66 | +<!-- description:end --> |
| 67 | + |
| 68 | +## Solutions |
| 69 | + |
| 70 | +<!-- solution:start --> |
| 71 | + |
| 72 | +### Solution 1: Counting |
| 73 | + |
| 74 | +We can use a hash table or an array $\textit{cnt}$ to record the occurrences of each character in the string $s$. Then, we traverse $\textit{cnt}$ to find the maximum frequency $a$ of characters that appear an odd number of times and the minimum frequency $b$ of characters that appear an even number of times. Finally, we return $a - b$. |
| 75 | + |
| 76 | +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set. In this problem, $|\Sigma| = 26$. |
| 77 | + |
| 78 | +<!-- tabs:start --> |
| 79 | + |
| 80 | +#### Python3 |
| 81 | + |
| 82 | +```python |
| 83 | +class Solution: |
| 84 | + def maxDifference(self, s: str) -> int: |
| 85 | + cnt = Counter(s) |
| 86 | + a, b = 0, inf |
| 87 | + for v in cnt.values(): |
| 88 | + if v % 2: |
| 89 | + a = max(a, v) |
| 90 | + else: |
| 91 | + b = min(b, v) |
| 92 | + return a - b |
| 93 | +``` |
| 94 | + |
| 95 | +#### Java |
| 96 | + |
| 97 | +```java |
| 98 | +class Solution { |
| 99 | + public int maxDifference(String s) { |
| 100 | + int[] cnt = new int[26]; |
| 101 | + for (char c : s.toCharArray()) { |
| 102 | + ++cnt[c - 'a']; |
| 103 | + } |
| 104 | + int a = 0, b = 1 << 30; |
| 105 | + for (int v : cnt) { |
| 106 | + if (v % 2 == 1) { |
| 107 | + a = Math.max(a, v); |
| 108 | + } else if (v > 0) { |
| 109 | + b = Math.min(b, v); |
| 110 | + } |
| 111 | + } |
| 112 | + return a - b; |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +#### C++ |
| 118 | + |
| 119 | +```cpp |
| 120 | +class Solution { |
| 121 | +public: |
| 122 | + int maxDifference(string s) { |
| 123 | + int cnt[26]{}; |
| 124 | + for (char c : s) { |
| 125 | + ++cnt[c - 'a']; |
| 126 | + } |
| 127 | + int a = 0, b = 1 << 30; |
| 128 | + for (int v : cnt) { |
| 129 | + if (v % 2 == 1) { |
| 130 | + a = max(a, v); |
| 131 | + } else if (v > 0) { |
| 132 | + b = min(b, v); |
| 133 | + } |
| 134 | + } |
| 135 | + return a - b; |
| 136 | + } |
| 137 | +}; |
| 138 | +``` |
| 139 | +
|
| 140 | +#### Go |
| 141 | +
|
| 142 | +```go |
| 143 | +func maxDifference(s string) int { |
| 144 | + cnt := [26]int{} |
| 145 | + for _, c := range s { |
| 146 | + cnt[c-'a']++ |
| 147 | + } |
| 148 | + a, b := 0, 1<<30 |
| 149 | + for _, v := range cnt { |
| 150 | + if v%2 == 1 { |
| 151 | + a = max(a, v) |
| 152 | + } else if v > 0 { |
| 153 | + b = min(b, v) |
| 154 | + } |
| 155 | + } |
| 156 | + return a - b |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +#### TypeScript |
| 161 | + |
| 162 | +```ts |
| 163 | +function maxDifference(s: string): number { |
| 164 | + const cnt: Record<string, number> = {}; |
| 165 | + for (const c of s) { |
| 166 | + cnt[c] = (cnt[c] || 0) + 1; |
| 167 | + } |
| 168 | + let [a, b] = [0, Infinity]; |
| 169 | + for (const [_, v] of Object.entries(cnt)) { |
| 170 | + if (v % 2 === 1) { |
| 171 | + a = Math.max(a, v); |
| 172 | + } else { |
| 173 | + b = Math.min(b, v); |
| 174 | + } |
| 175 | + } |
| 176 | + return a - b; |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +<!-- tabs:end --> |
| 181 | + |
| 182 | +<!-- solution:end --> |
| 183 | + |
| 184 | +<!-- problem:end --> |
0 commit comments