Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 091bff9

Browse files
feat: add solutions to lc problem: No.3035 (doocs#2340)
No.3035.Maximum Palindromes After Operations
1 parent 10f89f6 commit 091bff9

File tree

7 files changed

+313
-6
lines changed

7 files changed

+313
-6
lines changed

‎solution/3000-3099/3035.Maximum Palindromes After Operations/README.md‎

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,121 @@ words 中有一个回文 "a" 。
7171
<!-- tabs:start -->
7272

7373
```python
74-
74+
class Solution:
75+
def maxPalindromesAfterOperations(self, words: List[str]) -> int:
76+
s = mask = 0
77+
for w in words:
78+
s += len(w)
79+
for c in w:
80+
mask ^= 1 << (ord(c) - ord("a"))
81+
s -= mask.bit_count()
82+
words.sort(key=len)
83+
ans = 0
84+
for w in words:
85+
s -= len(w) // 2 * 2
86+
if s < 0:
87+
break
88+
ans += 1
89+
return ans
7590
```
7691

7792
```java
78-
93+
class Solution {
94+
public int maxPalindromesAfterOperations(String[] words) {
95+
int s = 0, mask = 0;
96+
for (var w : words) {
97+
s += w.length();
98+
for (var c : w.toCharArray()) {
99+
mask ^= 1 << (c - 'a');
100+
}
101+
}
102+
s -= Integer.bitCount(mask);
103+
Arrays.sort(words, (a, b) -> a.length() - b.length());
104+
int ans = 0;
105+
for (var w : words) {
106+
s -= w.length() / 2 * 2;
107+
if (s < 0) {
108+
break;
109+
}
110+
++ans;
111+
}
112+
return ans;
113+
}
114+
}
79115
```
80116

81117
```cpp
82-
118+
class Solution {
119+
public:
120+
int maxPalindromesAfterOperations(vector<string>& words) {
121+
int s = 0, mask = 0;
122+
for (const auto& w : words) {
123+
s += w.length();
124+
for (char c : w) {
125+
mask ^= 1 << (c - 'a');
126+
}
127+
}
128+
s -= __builtin_popcount(mask);
129+
sort(words.begin(), words.end(), [](const string& a, const string& b) { return a.length() < b.length(); });
130+
int ans = 0;
131+
for (const auto& w : words) {
132+
s -= w.length() / 2 * 2;
133+
if (s < 0) {
134+
break;
135+
}
136+
++ans;
137+
}
138+
return ans;
139+
}
140+
};
83141
```
84142
85143
```go
144+
func maxPalindromesAfterOperations(words []string) (ans int) {
145+
var s, mask int
146+
for _, w := range words {
147+
s += len(w)
148+
for _, c := range w {
149+
mask ^= 1 << (c - 'a')
150+
}
151+
}
152+
s -= bits.OnesCount(uint(mask))
153+
sort.Slice(words, func(i, j int) bool {
154+
return len(words[i]) < len(words[j])
155+
})
156+
for _, w := range words {
157+
s -= len(w) / 2 * 2
158+
if s < 0 {
159+
break
160+
}
161+
ans++
162+
}
163+
return
164+
}
165+
```
86166

167+
```ts
168+
function maxPalindromesAfterOperations(words: string[]): number {
169+
let s: number = 0;
170+
let mask: number = 0;
171+
for (const w of words) {
172+
s += w.length;
173+
for (const c of w) {
174+
mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
175+
}
176+
}
177+
s -= (mask.toString(2).match(/1/g) || []).length;
178+
words.sort((a, b) => a.length - b.length);
179+
let ans: number = 0;
180+
for (const w of words) {
181+
s -= Math.floor(w.length / 2) * 2;
182+
if (s < 0) {
183+
break;
184+
}
185+
ans++;
186+
}
187+
return ans;
188+
}
87189
```
88190

89191
<!-- tabs:end -->

‎solution/3000-3099/3035.Maximum Palindromes After Operations/README_EN.md‎

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,121 @@ Hence, the answer is 1.</pre>
6565
<!-- tabs:start -->
6666

6767
```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
6984
```
7085

7186
```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+
}
73109
```
74110

75111
```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+
};
77135
```
78136
79137
```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+
```
80160

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+
}
81183
```
82184

83185
<!-- tabs:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int maxPalindromesAfterOperations(vector<string>& words) {
4+
int s = 0, mask = 0;
5+
for (const auto& w : words) {
6+
s += w.length();
7+
for (char c : w) {
8+
mask ^= 1 << (c - 'a');
9+
}
10+
}
11+
s -= __builtin_popcount(mask);
12+
sort(words.begin(), words.end(), [](const string& a, const string& b) { return a.length() < b.length(); });
13+
int ans = 0;
14+
for (const auto& w : words) {
15+
s -= w.length() / 2 * 2;
16+
if (s < 0) {
17+
break;
18+
}
19+
++ans;
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func maxPalindromesAfterOperations(words []string) (ans int) {
2+
var s, mask int
3+
for _, w := range words {
4+
s += len(w)
5+
for _, c := range w {
6+
mask ^= 1 << (c - 'a')
7+
}
8+
}
9+
s -= bits.OnesCount(uint(mask))
10+
sort.Slice(words, func(i, j int) bool {
11+
return len(words[i]) < len(words[j])
12+
})
13+
for _, w := range words {
14+
s -= len(w) / 2 * 2
15+
if s < 0 {
16+
break
17+
}
18+
ans++
19+
}
20+
return
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int maxPalindromesAfterOperations(String[] words) {
3+
int s = 0, mask = 0;
4+
for (var w : words) {
5+
s += w.length();
6+
for (var c : w.toCharArray()) {
7+
mask ^= 1 << (c - 'a');
8+
}
9+
}
10+
s -= Integer.bitCount(mask);
11+
Arrays.sort(words, (a, b) -> a.length() - b.length());
12+
int ans = 0;
13+
for (var w : words) {
14+
s -= w.length() / 2 * 2;
15+
if (s < 0) {
16+
break;
17+
}
18+
++ans;
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def maxPalindromesAfterOperations(self, words: List[str]) -> int:
3+
s = mask = 0
4+
for w in words:
5+
s += len(w)
6+
for c in w:
7+
mask ^= 1 << (ord(c) - ord("a"))
8+
s -= mask.bit_count()
9+
words.sort(key=len)
10+
ans = 0
11+
for w in words:
12+
s -= len(w) // 2 * 2
13+
if s < 0:
14+
break
15+
ans += 1
16+
return ans
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function maxPalindromesAfterOperations(words: string[]): number {
2+
let s: number = 0;
3+
let mask: number = 0;
4+
for (const w of words) {
5+
s += w.length;
6+
for (const c of w) {
7+
mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
8+
}
9+
}
10+
s -= (mask.toString(2).match(/1/g) || []).length;
11+
words.sort((a, b) => a.length - b.length);
12+
let ans: number = 0;
13+
for (const w of words) {
14+
s -= Math.floor(w.length / 2) * 2;
15+
if (s < 0) {
16+
break;
17+
}
18+
ans++;
19+
}
20+
return ans;
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /