Skip to content

Commit 36ca738

Browse files
authored
Merge pull request #1498 from VaishnaviMankala19/lc-sol-3035
added 3035 lc-solution
2 parents f9c771a + 40d4c0c commit 36ca738

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
id: maximum-palindromes-after-operations
3+
title: Maximum Palindromes After Operations (LeetCode)
4+
sidebar_label: 3035-MaximumPalindromesAfterOperations
5+
tags:
6+
- Array
7+
- String
8+
- Greedy
9+
description: Determine the maximum number of palindromes that can be formed from an array of strings after performing some operations.
10+
sidebar_position: 3035
11+
---
12+
13+
## Problem Description
14+
15+
| Problem Statement | Solution Link | LeetCode Profile |
16+
| :---------------- | :------------ | :--------------- |
17+
| [Maximum Palindromes After Operations](https://leetcode.com/problems/maximum-palindromes-after-operations/) | [Maximum Palindromes After Operations Solution on LeetCode](https://leetcode.com/problems/maximum-palindromes-after-operations/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) |
18+
19+
## Problem Description
20+
21+
- You are given a 0-indexed string array words having length n and containing 0-indexed strings.
22+
23+
- You are allowed to perform the following operation any number of times (including zero):
24+
25+
- Choose integers i, j, x, and y such that `0 <= i`, `j < n`, `0 <= x < words[i].length`, `0 <= y < words[j].length`, and swap the characters words[i][x] and words[j][y].
26+
27+
- Return an integer denoting the maximum number of palindromes words can contain, after performing some operations.
28+
29+
- Note: i and j may be equal during an operation.
30+
31+
### Example 1
32+
33+
- **Input:** `words = ["abbb","ba","aa"]`
34+
- **Output:** `3`
35+
- **Explanation:** In this example, one way to get the maximum number of palindromes is:
36+
Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"].
37+
All strings in words are now palindromes.
38+
Hence, the maximum number of palindromes achievable is 3.
39+
40+
### Example 2
41+
42+
- **Input:** `words = ["abc","ab"]`
43+
- **Output:** `2`
44+
- **Explanation:** In this example, one way to get the maximum number of palindromes is:
45+
Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"].
46+
Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"].
47+
Both strings are now palindromes.
48+
Hence, the maximum number of palindromes achievable is 2.
49+
50+
### Example 3
51+
52+
- **Input:** `words = ["cd","ef","a"]`
53+
- **Output:** `1`
54+
- **Explanation:** In this example, there is no need to perform any operation.
55+
There is one palindrome in words "a".
56+
It can be shown that it is not possible to get more than one palindrome after any number of operations.
57+
Hence, the answer is 1.
58+
59+
### Constraints
60+
61+
- `1 <= words.length <= 1000`
62+
- `1 <= words[i].length <= 100`
63+
- `words[i]` consists only of lowercase English letters.
64+
65+
## Approach
66+
67+
To solve this problem, we can use a greedy approach to determine the maximum number of palindromes that can be formed after performing some operations. Here's the approach:
68+
69+
1. Identify characters that can be swapped to form palindromes.
70+
2. Use a frequency map to count the occurrences of each character.
71+
3. Calculate the maximum number of palindromes by ensuring each palindrome has at most one odd-frequency character.
72+
73+
### Solution Code
74+
75+
#### Python
76+
77+
```python
78+
from collections import Counter
79+
80+
class Solution:
81+
def maxPalindromesAfterOperations(self, words: List[str]) -> int:
82+
char_count = Counter()
83+
for word in words:
84+
char_count.update(word)
85+
86+
odd_count = sum(1 for count in char_count.values() if count % 2 != 0)
87+
88+
return len(words) - odd_count // 2
89+
```
90+
91+
#### C++
92+
93+
```c++
94+
#include <vector>
95+
#include <string>
96+
#include <unordered_map>
97+
using namespace std;
98+
99+
class Solution {
100+
public:
101+
int maxPalindromesAfterOperations(vector<string>& words) {
102+
unordered_map<char, int> char_count;
103+
for (const string& word : words) {
104+
for (char c : word) {
105+
char_count[c]++;
106+
}
107+
}
108+
109+
int odd_count = 0;
110+
for (const auto& pair : char_count) {
111+
if (pair.second % 2 != 0) {
112+
odd_count++;
113+
}
114+
}
115+
116+
return words.size() - odd_count / 2;
117+
}
118+
};
119+
```
120+
121+
#### Java
122+
123+
```java
124+
import java.util.HashMap;
125+
import java.util.Map;
126+
127+
class Solution {
128+
public int maxPalindromesAfterOperations(String[] words) {
129+
Map<Character, Integer> charCount = new HashMap<>();
130+
for (String word : words) {
131+
for (char c : word.toCharArray()) {
132+
charCount.put(c, charCount.getOrDefault(c, 0) + 1);
133+
}
134+
}
135+
136+
int oddCount = 0;
137+
for (int count : charCount.values()) {
138+
if (count % 2 != 0) {
139+
oddCount++;
140+
}
141+
}
142+
143+
return words.length - oddCount / 2;
144+
}
145+
}
146+
```
147+
148+
### Conclusion
149+
The solutions use a greedy approach to determine the maximum number of palindromes that can be
150+
formed by leveraging a frequency map to count character occurrences. This ensures an efficient and
151+
straightforward way to solve the problem across different programming languages.

0 commit comments

Comments
 (0)