File tree Expand file tree Collapse file tree 4 files changed +125
-0
lines changed
solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I Expand file tree Collapse file tree 4 files changed +125
-0
lines changed Original file line number Diff line number Diff line change @@ -187,6 +187,51 @@ function maxDifference(s: string): number {
187
187
}
188
188
```
189
189
190
+ #### Rust
191
+
192
+ ``` rust
193
+ impl Solution {
194
+ pub fn max_difference (s : String ) -> i32 {
195
+ let mut cnt = [0 ; 26 ];
196
+ for c in s . bytes () {
197
+ cnt [(c - b 'a' ) as usize ] += 1 ;
198
+ }
199
+ let mut a = 0 ;
200
+ let mut b = 1 << 30 ;
201
+ for & v in cnt . iter () {
202
+ if v % 2 == 1 {
203
+ a = a . max (v );
204
+ } else if v > 0 {
205
+ b = b . min (v );
206
+ }
207
+ }
208
+ a - b
209
+ }
210
+ }
211
+ ```
212
+
213
+ #### C#
214
+
215
+ ``` cs
216
+ public class Solution {
217
+ public int MaxDifference (string s ) {
218
+ int [] cnt = new int [26 ];
219
+ foreach (char c in s ) {
220
+ ++ cnt [c - 'a' ];
221
+ }
222
+ int a = 0 , b = 1 << 30 ;
223
+ foreach (int v in cnt ) {
224
+ if (v % 2 == 1 ) {
225
+ a = Math .Max (a , v );
226
+ } else if (v > 0 ) {
227
+ b = Math .Min (b , v );
228
+ }
229
+ }
230
+ return a - b ;
231
+ }
232
+ }
233
+ ```
234
+
190
235
<!-- tabs: end -->
191
236
192
237
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -185,6 +185,51 @@ function maxDifference(s: string): number {
185
185
}
186
186
```
187
187
188
+ #### Rust
189
+
190
+ ``` rust
191
+ impl Solution {
192
+ pub fn max_difference (s : String ) -> i32 {
193
+ let mut cnt = [0 ; 26 ];
194
+ for c in s . bytes () {
195
+ cnt [(c - b 'a' ) as usize ] += 1 ;
196
+ }
197
+ let mut a = 0 ;
198
+ let mut b = 1 << 30 ;
199
+ for & v in cnt . iter () {
200
+ if v % 2 == 1 {
201
+ a = a . max (v );
202
+ } else if v > 0 {
203
+ b = b . min (v );
204
+ }
205
+ }
206
+ a - b
207
+ }
208
+ }
209
+ ```
210
+
211
+ #### C#
212
+
213
+ ``` cs
214
+ public class Solution {
215
+ public int MaxDifference (string s ) {
216
+ int [] cnt = new int [26 ];
217
+ foreach (char c in s ) {
218
+ ++ cnt [c - 'a' ];
219
+ }
220
+ int a = 0 , b = 1 << 30 ;
221
+ foreach (int v in cnt ) {
222
+ if (v % 2 == 1 ) {
223
+ a = Math .Max (a , v );
224
+ } else if (v > 0 ) {
225
+ b = Math .Min (b , v );
226
+ }
227
+ }
228
+ return a - b ;
229
+ }
230
+ }
231
+ ```
232
+
188
233
<!-- tabs: end -->
189
234
190
235
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int MaxDifference ( string s ) {
3
+ int [ ] cnt = new int [ 26 ] ;
4
+ foreach ( char c in s ) {
5
+ ++ cnt [ c - 'a' ] ;
6
+ }
7
+ int a = 0 , b = 1 << 30 ;
8
+ foreach ( int v in cnt ) {
9
+ if ( v % 2 == 1 ) {
10
+ a = Math . Max ( a , v ) ;
11
+ } else if ( v > 0 ) {
12
+ b = Math . Min ( b , v ) ;
13
+ }
14
+ }
15
+ return a - b ;
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn max_difference ( s : String ) -> i32 {
3
+ let mut cnt = [ 0 ; 26 ] ;
4
+ for c in s. bytes ( ) {
5
+ cnt[ ( c - b'a' ) as usize ] += 1 ;
6
+ }
7
+ let mut a = 0 ;
8
+ let mut b = 1 << 30 ;
9
+ for & v in cnt. iter ( ) {
10
+ if v % 2 == 1 {
11
+ a = a. max ( v) ;
12
+ } else if v > 0 {
13
+ b = b. min ( v) ;
14
+ }
15
+ }
16
+ a - b
17
+ }
18
+ }
You can’t perform that action at this time.
0 commit comments