Skip to content

Commit 3191213

Browse files
committed
110125
1 parent b1c6bba commit 3191213

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# leetcode submit region begin(Prohibit modification and deletion)
2+
3+
from collections import Counter
4+
5+
6+
class Solution:
7+
def canConstruct(self, s: str, k: int) -> bool:
8+
# If k is greater than the length of the string, it's impossible
9+
if k > len(s):
10+
return False
11+
12+
# Count the frequency of each character in the string
13+
char_counts = Counter(s)
14+
15+
# Count how many characters have odd frequencies
16+
num_odd = sum(1 for count in char_counts.values() if count % 2 == 1)
17+
18+
# The minimum number of palindromic substrings needed is the number of odd counts
19+
min_k = num_odd
20+
21+
# The maximum number of palindromic substrings is the length of the string
22+
max_k = len(s)
23+
24+
# Check if k is within the feasible range
25+
return min_k <= k <= max_k
26+
27+
28+
# leetcode submit region end(Prohibit modification and deletion)
29+
30+
31+
class ConstructKPalindromeStrings(Solution):
32+
pass

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from leetcode.editor.en.Q3042.CountPrefixAndSuffixPairsI import CountPrefixAndSuffixPairsI
1+
from leetcode.editor.en.Q1400.ConstructKPalindromeStrings import ConstructKPalindromeStrings
22

33
if __name__ == '__main__':
4-
print(CountPrefixAndSuffixPairsI().countPrefixSuffixPairs(["a", "aba", "ababa", "aa"]))
4+
print(ConstructKPalindromeStrings().canConstruct(s="eminem", k=2))

0 commit comments

Comments
 (0)