3442. Maximum Difference Between Even and Odd Frequency I LeetCode Solution
In this guide, you will get 3442. Maximum Difference Between Even and Odd Frequency I LeetCode Solution with the best time and space complexity. The solution to Maximum Difference Between Even and Odd Frequency I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Difference Between Even and Odd Frequency I solution in C++
- Maximum Difference Between Even and Odd Frequency I solution in Java
- Maximum Difference Between Even and Odd Frequency I solution in Python
- Additional Resources
Problem Statement of Maximum Difference Between Even and Odd Frequency I
You are given a string s consisting of lowercase English letters. Your task is to find the maximum difference between the frequency of two characters in the string such that:
One of the characters has an even frequency in the string.
The other character has an odd frequency in the string.
Return the maximum difference, calculated as the frequency of the character with an odd frequency minus the frequency of the character with an even frequency.
Example 1:
Input: s = “aaaaabbc”
Output: 3
Explanation:
The character ‘a’ has an odd frequency of 5, and ‘b’ has an even frequency of 2.
The maximum difference is 5 – 2 = 3.
Example 2:
Input: s = “abcabcab”
Output: 1
Explanation:
The character ‘a’ has an odd frequency of 3, and ‘c’ has an even frequency of 2.
The maximum difference is 3 – 2 = 1.
Constraints:
3 <= s.length <= 100
s consists only of lowercase English letters.
s contains at least one character with an odd frequency and one with an even frequency.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(26) = O(1)
3442. Maximum Difference Between Even and Odd Frequency I LeetCode Solution in C++
class Solution {
public:
int maxDifference(string s) {
vector<int> count(26);
int maxOdd = 0;
int minEven = s.length();
for (const char c : s)
++count[c - 'a'];
for (const int freq : count) {
if (freq == 0)
continue;
if (freq % 2 == 0)
minEven = min(minEven, freq);
else
maxOdd = max(maxOdd, freq);
}
return maxOdd - minEven;
}
};
/* code provided by PROGIEZ */
3442. Maximum Difference Between Even and Odd Frequency I LeetCode Solution in Java
class Solution {
public int maxDifference(String s) {
int[] count = new int[26];
int maxOdd = 0;
int minEven = s.length();
for (final char c : s.toCharArray())
++count[c - 'a'];
for (final int freq : count) {
if (freq == 0)
continue;
if (freq % 2 == 0)
minEven = Math.min(minEven, freq);
else
maxOdd = Math.max(maxOdd, freq);
}
return maxOdd - minEven;
}
}
// code provided by PROGIEZ
3442. Maximum Difference Between Even and Odd Frequency I LeetCode Solution in Python
class Solution:
def maxDifference(self, s: str) -> int:
count = collections.Counter(s)
maxOdd = max((freq for freq in count.values()
if freq % 2 == 1), default=0)
minEven = min((freq for freq in count.values()
if freq % 2 == 0), default=len(s))
return maxOdd - minEven
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.