diff --git a/solution/3000-3099/3035.Maximum Palindromes After Operations/README.md b/solution/3000-3099/3035.Maximum Palindromes After Operations/README.md index fa637e02c7a62..432479e3de542 100644 --- a/solution/3000-3099/3035.Maximum Palindromes After Operations/README.md +++ b/solution/3000-3099/3035.Maximum Palindromes After Operations/README.md @@ -71,19 +71,121 @@ words 中有一个回文 "a" 。 ```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& 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; +} ``` diff --git a/solution/3000-3099/3035.Maximum Palindromes After Operations/README_EN.md b/solution/3000-3099/3035.Maximum Palindromes After Operations/README_EN.md index 660115d59c399..fd6b9683a09d6 100644 --- a/solution/3000-3099/3035.Maximum Palindromes After Operations/README_EN.md +++ b/solution/3000-3099/3035.Maximum Palindromes After Operations/README_EN.md @@ -65,19 +65,121 @@ Hence, the answer is 1. ```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& 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; +} ``` diff --git a/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.cpp b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.cpp new file mode 100644 index 0000000000000..be5ce419762b9 --- /dev/null +++ b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int maxPalindromesAfterOperations(vector& 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; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.go b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.go new file mode 100644 index 0000000000000..0f2c1e7053b0f --- /dev/null +++ b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.java b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.java new file mode 100644 index 0000000000000..0e5034aadb9bc --- /dev/null +++ b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.py b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.py new file mode 100644 index 0000000000000..675a2c12c37ac --- /dev/null +++ b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.py @@ -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 diff --git a/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.ts b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.ts new file mode 100644 index 0000000000000..6e8dd8f5e7d27 --- /dev/null +++ b/solution/3000-3099/3035.Maximum Palindromes After Operations/Solution.ts @@ -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; +}