@@ -65,19 +65,121 @@ Hence, the answer is 1.</pre>
65
65
<!-- tabs:start -->
66
66
67
67
``` python
68
-
68
+ class Solution :
69
+ def maxPalindromesAfterOperations (self , words : List[str ]) -> int :
70
+ s = mask = 0
71
+ for w in words:
72
+ s += len (w)
73
+ for c in w:
74
+ mask ^= 1 << (ord (c) - ord (" a" ))
75
+ s -= mask.bit_count()
76
+ words.sort(key = len )
77
+ ans = 0
78
+ for w in words:
79
+ s -= len (w) // 2 * 2
80
+ if s < 0 :
81
+ break
82
+ ans += 1
83
+ return ans
69
84
```
70
85
71
86
``` java
72
-
87
+ class Solution {
88
+ public int maxPalindromesAfterOperations (String [] words ) {
89
+ int s = 0 , mask = 0 ;
90
+ for (var w : words) {
91
+ s += w. length();
92
+ for (var c : w. toCharArray()) {
93
+ mask ^ = 1 << (c - ' a' );
94
+ }
95
+ }
96
+ s -= Integer . bitCount(mask);
97
+ Arrays . sort(words, (a, b) - > a. length() - b. length());
98
+ int ans = 0 ;
99
+ for (var w : words) {
100
+ s -= w. length() / 2 * 2 ;
101
+ if (s < 0 ) {
102
+ break ;
103
+ }
104
+ ++ ans;
105
+ }
106
+ return ans;
107
+ }
108
+ }
73
109
```
74
110
75
111
``` cpp
76
-
112
+ class Solution {
113
+ public:
114
+ int maxPalindromesAfterOperations(vector<string >& words) {
115
+ int s = 0, mask = 0;
116
+ for (const auto& w : words) {
117
+ s += w.length();
118
+ for (char c : w) {
119
+ mask ^= 1 << (c - 'a');
120
+ }
121
+ }
122
+ s -= __ builtin_popcount(mask);
123
+ sort(words.begin(), words.end(), [ ] (const string& a, const string& b) { return a.length() < b.length(); });
124
+ int ans = 0;
125
+ for (const auto& w : words) {
126
+ s -= w.length() / 2 * 2;
127
+ if (s < 0) {
128
+ break;
129
+ }
130
+ ++ans;
131
+ }
132
+ return ans;
133
+ }
134
+ };
77
135
```
78
136
79
137
```go
138
+ func maxPalindromesAfterOperations(words []string) (ans int) {
139
+ var s, mask int
140
+ for _, w := range words {
141
+ s += len(w)
142
+ for _, c := range w {
143
+ mask ^= 1 << (c - 'a')
144
+ }
145
+ }
146
+ s -= bits.OnesCount(uint(mask))
147
+ sort.Slice(words, func(i, j int) bool {
148
+ return len(words[i]) < len(words[j])
149
+ })
150
+ for _, w := range words {
151
+ s -= len(w) / 2 * 2
152
+ if s < 0 {
153
+ break
154
+ }
155
+ ans++
156
+ }
157
+ return
158
+ }
159
+ ```
80
160
161
+ ``` ts
162
+ function maxPalindromesAfterOperations(words : string []): number {
163
+ let s: number = 0 ;
164
+ let mask: number = 0 ;
165
+ for (const w of words ) {
166
+ s += w .length ;
167
+ for (const c of w ) {
168
+ mask ^= 1 << (c .charCodeAt (0 ) - ' a' .charCodeAt (0 ));
169
+ }
170
+ }
171
+ s -= (mask .toString (2 ).match (/ 1/ g ) || []).length ;
172
+ words .sort ((a , b ) => a .length - b .length );
173
+ let ans: number = 0 ;
174
+ for (const w of words ) {
175
+ s -= Math .floor (w .length / 2 ) * 2 ;
176
+ if (s < 0 ) {
177
+ break ;
178
+ }
179
+ ans ++ ;
180
+ }
181
+ return ans ;
182
+ }
81
183
```
82
184
83
185
<!-- tabs: end -->
0 commit comments