herrDeng網內搜尋

自訂搜尋

Ads

2025年1月11日 星期六

Python C++速解Leetcode迴文1400 Construct K Palindrome Strings



Python C++速解Leetcode迴文1400  Construct K Palindrome Strings
關鍵:偶數長度的回文字串中所有字母都出現偶數,奇數長度的回文字串中除了一個例外其他所有字母都出現偶數。
----------
The key observation: palindrome strings of even lengths have all letters even occurrences, palindrome strings of odd lengths have all letters even occurrences but except one letter with odd occurrence.
[Py3解答請進]

[C++ python使用DFS與奇偶測試解Leetcode1457 Pseudo Palindromic Paths in a Binary Tree]https://www.youtube.com/watch?v=StHXyYgavUU
[C, C++ ,python速解迴文Leetcode 2108 Find First Palindromic String in the Array]https://www.youtube.com/watch?v=eXhdv8jDpao

  1. class Solution:
  2. def canConstruct(self, s: str, k: int) -> bool:
  3. n=len(s)
  4. if n<k: return False
  5. freq=[False]*26
  6. for c in s:
  7. freq[ord(c)-97]^=1
  8. return freq.count(True)<=k

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章