Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.3035 #2340

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 105 additions & 3 deletions solution/3000-3099/3035.Maximum Palindromes After Operations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,121 @@ words 中有一个回文 "a" 。
<!-- tabs:start -->

```python

class Solution:
def maxPalindromesAfterOperations(self, words: List[str]) -> int:
s = mask = 0
for w in words:
s += len(w)
for c in w:
mask ^= 1 << (ord(c) - ord("a"))
s -= mask.bit_count()
words.sort(key=len)
ans = 0
for w in words:
s -= len(w) // 2 * 2
if s < 0:
break
ans += 1
return ans
```

```java

class Solution {
public int maxPalindromesAfterOperations(String[] words) {
int s = 0, mask = 0;
for (var w : words) {
s += w.length();
for (var c : w.toCharArray()) {
mask ^= 1 << (c - 'a');
}
}
s -= Integer.bitCount(mask);
Arrays.sort(words, (a, b) -> a.length() - b.length());
int ans = 0;
for (var w : words) {
s -= w.length() / 2 * 2;
if (s < 0) {
break;
}
++ans;
}
return ans;
}
}
```

```cpp

class Solution {
public:
int maxPalindromesAfterOperations(vector<string>& words) {
int s = 0, mask = 0;
for (const auto& w : words) {
s += w.length();
for (char c : w) {
mask ^= 1 << (c - 'a');
}
}
s -= __builtin_popcount(mask);
sort(words.begin(), words.end(), [](const string& a, const string& b) { return a.length() < b.length(); });
int ans = 0;
for (const auto& w : words) {
s -= w.length() / 2 * 2;
if (s < 0) {
break;
}
++ans;
}
return ans;
}
};
```

```go
func maxPalindromesAfterOperations(words []string) (ans int) {
var s, mask int
for _, w := range words {
s += len(w)
for _, c := range w {
mask ^= 1 << (c - 'a')
}
}
s -= bits.OnesCount(uint(mask))
sort.Slice(words, func(i, j int) bool {
return len(words[i]) < len(words[j])
})
for _, w := range words {
s -= len(w) / 2 * 2
if s < 0 {
break
}
ans++
}
return
}
```

```ts
function maxPalindromesAfterOperations(words: string[]): number {
let s: number = 0;
let mask: number = 0;
for (const w of words) {
s += w.length;
for (const c of w) {
mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
}
}
s -= (mask.toString(2).match(/1/g) || []).length;
words.sort((a, b) => a.length - b.length);
let ans: number = 0;
for (const w of words) {
s -= Math.floor(w.length / 2) * 2;
if (s < 0) {
break;
}
ans++;
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,121 @@ Hence, the answer is 1.</pre>
<!-- tabs:start -->

```python

class Solution:
def maxPalindromesAfterOperations(self, words: List[str]) -> int:
s = mask = 0
for w in words:
s += len(w)
for c in w:
mask ^= 1 << (ord(c) - ord("a"))
s -= mask.bit_count()
words.sort(key=len)
ans = 0
for w in words:
s -= len(w) // 2 * 2
if s < 0:
break
ans += 1
return ans
```

```java

class Solution {
public int maxPalindromesAfterOperations(String[] words) {
int s = 0, mask = 0;
for (var w : words) {
s += w.length();
for (var c : w.toCharArray()) {
mask ^= 1 << (c - 'a');
}
}
s -= Integer.bitCount(mask);
Arrays.sort(words, (a, b) -> a.length() - b.length());
int ans = 0;
for (var w : words) {
s -= w.length() / 2 * 2;
if (s < 0) {
break;
}
++ans;
}
return ans;
}
}
```

```cpp

class Solution {
public:
int maxPalindromesAfterOperations(vector<string>& words) {
int s = 0, mask = 0;
for (const auto& w : words) {
s += w.length();
for (char c : w) {
mask ^= 1 << (c - 'a');
}
}
s -= __builtin_popcount(mask);
sort(words.begin(), words.end(), [](const string& a, const string& b) { return a.length() < b.length(); });
int ans = 0;
for (const auto& w : words) {
s -= w.length() / 2 * 2;
if (s < 0) {
break;
}
++ans;
}
return ans;
}
};
```

```go
func maxPalindromesAfterOperations(words []string) (ans int) {
var s, mask int
for _, w := range words {
s += len(w)
for _, c := range w {
mask ^= 1 << (c - 'a')
}
}
s -= bits.OnesCount(uint(mask))
sort.Slice(words, func(i, j int) bool {
return len(words[i]) < len(words[j])
})
for _, w := range words {
s -= len(w) / 2 * 2
if s < 0 {
break
}
ans++
}
return
}
```

```ts
function maxPalindromesAfterOperations(words: string[]): number {
let s: number = 0;
let mask: number = 0;
for (const w of words) {
s += w.length;
for (const c of w) {
mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
}
}
s -= (mask.toString(2).match(/1/g) || []).length;
words.sort((a, b) => a.length - b.length);
let ans: number = 0;
for (const w of words) {
s -= Math.floor(w.length / 2) * 2;
if (s < 0) {
break;
}
ans++;
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int maxPalindromesAfterOperations(vector<string>& words) {
int s = 0, mask = 0;
for (const auto& w : words) {
s += w.length();
for (char c : w) {
mask ^= 1 << (c - 'a');
}
}
s -= __builtin_popcount(mask);
sort(words.begin(), words.end(), [](const string& a, const string& b) { return a.length() < b.length(); });
int ans = 0;
for (const auto& w : words) {
s -= w.length() / 2 * 2;
if (s < 0) {
break;
}
++ans;
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
func maxPalindromesAfterOperations(words []string) (ans int) {
var s, mask int
for _, w := range words {
s += len(w)
for _, c := range w {
mask ^= 1 << (c - 'a')
}
}
s -= bits.OnesCount(uint(mask))
sort.Slice(words, func(i, j int) bool {
return len(words[i]) < len(words[j])
})
for _, w := range words {
s -= len(w) / 2 * 2
if s < 0 {
break
}
ans++
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int maxPalindromesAfterOperations(String[] words) {
int s = 0, mask = 0;
for (var w : words) {
s += w.length();
for (var c : w.toCharArray()) {
mask ^= 1 << (c - 'a');
}
}
s -= Integer.bitCount(mask);
Arrays.sort(words, (a, b) -> a.length() - b.length());
int ans = 0;
for (var w : words) {
s -= w.length() / 2 * 2;
if (s < 0) {
break;
}
++ans;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def maxPalindromesAfterOperations(self, words: List[str]) -> int:
s = mask = 0
for w in words:
s += len(w)
for c in w:
mask ^= 1 << (ord(c) - ord("a"))
s -= mask.bit_count()
words.sort(key=len)
ans = 0
for w in words:
s -= len(w) // 2 * 2
if s < 0:
break
ans += 1
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function maxPalindromesAfterOperations(words: string[]): number {
let s: number = 0;
let mask: number = 0;
for (const w of words) {
s += w.length;
for (const c of w) {
mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
}
}
s -= (mask.toString(2).match(/1/g) || []).length;
words.sort((a, b) => a.length - b.length);
let ans: number = 0;
for (const w of words) {
s -= Math.floor(w.length / 2) * 2;
if (s < 0) {
break;
}
ans++;
}
return ans;
}
Loading