Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8580247

Browse files
authored
Create Maximum Difference Between Even and Odd Frequency I.py
1 parent db5b995 commit 8580247

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
You are given a string s consisting of lowercase English letters.
2+
3+
Your task is to find the maximum difference diff = a1 - a2 between the frequency of characters a1 and a2 in the string such that:
4+
5+
a1 has an odd frequency in the string.
6+
a2 has an even frequency in the string.
7+
Return this maximum difference.
8+
-----------------------------------------------------
9+
10+
class Solution:
11+
def maxDifference(self, s: str) -> int:
12+
freq = dict(Counter(s))
13+
14+
freq = [[k,v] for k,v in freq.items()]
15+
freq.sort(key = lambda x: x[1])
16+
17+
freq = [v for k,v in freq]
18+
max_odd = max(list(filter(lambda x: x%2 == 1,freq)))
19+
20+
min_ev = min(list(filter(lambda x: x%2 == 0, freq)))
21+
return max_odd-min_ev

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /