266. Palindrome Permutation

Easy


Given a string s, return true if a permutation of the string could form a palindrome.

 

Example 1:

Input: s = "code"
Output: false

Example 2:

Input: s = "aab"
Output: true

Example 3:

Input: s = "carerac"
Output: true

 

Constraints:

  • 1 <= s.length <= 5000
  • s consists of only lowercase English letters.




 class Solution:
    def canPermutePalindrome(self, string: str) -> bool:

        counter = Counter(self.clean(string))
        return sum(c%2 for c in counter.values()) <= 1

    def clean(self, s):
        return [c for c in s.lower() if c in string.ascii_lowercase]    

Random Note


time.perf_counter() always returns the float value of time in seconds. while pref_counter_ns() always gives the integer value of time in nanoseconds.


t1_start = perf_counter()
t1_stop = perf_counter()
print("Elapsed time:", t1_stop, t1_start)