-
-
Notifications
You must be signed in to change notification settings - Fork 155
added 3035 lc-solution #1498
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
Merged
ajay-dhangar
merged 7 commits into
codeharborhub:main
from
VaishnaviMankala19:lc-sol-3035
Jun 17, 2024
Merged
added 3035 lc-solution #1498
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5d87caa
added 3035 lc-solution
VaishnaviMankala19 8291c24
Merge branch 'CodeHarborHub:main' into lc-sol-3035
VaishnaviMankala19 159c8b3
Merge branch 'CodeHarborHub:main' into lc-sol-3035
VaishnaviMankala19 0e61d70
Merge branch 'CodeHarborHub:main' into lc-sol-3035
VaishnaviMankala19 3f9f184
Merge branch 'CodeHarborHub:main' into lc-sol-3035
VaishnaviMankala19 6c5bb24
Merge branch 'CodeHarborHub:main' into lc-sol-3035
VaishnaviMankala19 40d4c0c
Updated 3035-Maximum-Palindromes-After-Operations.md
VaishnaviMankala19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
dsa-solutions/lc-solutions/3000-3099/3035-Maximum-Palindromes-After-Operations.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
--- | ||
id: maximum-palindromes-after-operations | ||
title: Maximum Palindromes After Operations (LeetCode) | ||
sidebar_label: 3035-MaximumPalindromesAfterOperations | ||
tags: | ||
- Array | ||
- String | ||
- Greedy | ||
description: Determine the maximum number of palindromes that can be formed from an array of strings after performing some operations. | ||
sidebar_position: 3035 | ||
--- | ||
|
||
## Problem Description | ||
|
||
| Problem Statement | Solution Link | LeetCode Profile | | ||
| :---------------- | :------------ | :--------------- | | ||
| [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/) | | ||
|
||
## Problem Description | ||
|
||
- You are given a 0-indexed string array words having length n and containing 0-indexed strings. | ||
|
||
- You are allowed to perform the following operation any number of times (including zero): | ||
|
||
- 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]. | ||
|
||
- Return an integer denoting the maximum number of palindromes words can contain, after performing some operations. | ||
|
||
- Note: i and j may be equal during an operation. | ||
|
||
### Example 1 | ||
|
||
- **Input:** `words = ["abbb","ba","aa"]` | ||
- **Output:** `3` | ||
- **Explanation:** In this example, one way to get the maximum number of palindromes is: | ||
Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"]. | ||
All strings in words are now palindromes. | ||
Hence, the maximum number of palindromes achievable is 3. | ||
|
||
### Example 2 | ||
|
||
- **Input:** `words = ["abc","ab"]` | ||
- **Output:** `2` | ||
- **Explanation:** In this example, one way to get the maximum number of palindromes is: | ||
Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"]. | ||
Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"]. | ||
Both strings are now palindromes. | ||
Hence, the maximum number of palindromes achievable is 2. | ||
|
||
### Example 3 | ||
|
||
- **Input:** `words = ["cd","ef","a"]` | ||
- **Output:** `1` | ||
- **Explanation:** In this example, there is no need to perform any operation. | ||
There is one palindrome in words "a". | ||
It can be shown that it is not possible to get more than one palindrome after any number of operations. | ||
Hence, the answer is 1. | ||
|
||
### Constraints | ||
|
||
- `1 <= words.length <= 1000` | ||
- `1 <= words[i].length <= 100` | ||
- `words[i]` consists only of lowercase English letters. | ||
|
||
## Approach | ||
|
||
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: | ||
|
||
1. Identify characters that can be swapped to form palindromes. | ||
2. Use a frequency map to count the occurrences of each character. | ||
3. Calculate the maximum number of palindromes by ensuring each palindrome has at most one odd-frequency character. | ||
|
||
### Solution Code | ||
|
||
#### Python | ||
|
||
```python | ||
from collections import Counter | ||
|
||
class Solution: | ||
def maxPalindromesAfterOperations(self, words: List[str]) -> int: | ||
char_count = Counter() | ||
for word in words: | ||
char_count.update(word) | ||
|
||
odd_count = sum(1 for count in char_count.values() if count % 2 != 0) | ||
|
||
return len(words) - odd_count // 2 | ||
``` | ||
|
||
#### C++ | ||
|
||
```c++ | ||
#include <vector> | ||
#include <string> | ||
#include <unordered_map> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int maxPalindromesAfterOperations(vector<string>& words) { | ||
unordered_map<char, int> char_count; | ||
for (const string& word : words) { | ||
for (char c : word) { | ||
char_count[c]++; | ||
} | ||
} | ||
|
||
int odd_count = 0; | ||
for (const auto& pair : char_count) { | ||
if (pair.second % 2 != 0) { | ||
odd_count++; | ||
} | ||
} | ||
|
||
return words.size() - odd_count / 2; | ||
} | ||
}; | ||
``` | ||
|
||
#### Java | ||
|
||
```java | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
public int maxPalindromesAfterOperations(String[] words) { | ||
Map<Character, Integer> charCount = new HashMap<>(); | ||
for (String word : words) { | ||
for (char c : word.toCharArray()) { | ||
charCount.put(c, charCount.getOrDefault(c, 0) + 1); | ||
} | ||
} | ||
|
||
int oddCount = 0; | ||
for (int count : charCount.values()) { | ||
if (count % 2 != 0) { | ||
oddCount++; | ||
} | ||
} | ||
|
||
return words.length - oddCount / 2; | ||
} | ||
} | ||
``` | ||
|
||
### Conclusion | ||
The solutions use a greedy approach to determine the maximum number of palindromes that can be | ||
formed by leveraging a frequency map to count character occurrences. This ensures an efficient and | ||
straightforward way to solve the problem across different programming languages. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.