Given a string s, return true if a permutation of the string could form a palindrome and false otherwise.
A palindrome is a string that reads the same forward and backward.
Example 1:
Input: s = "code"
Output: falseExample 2:
Input: s = "aab"
Output: trueExample 3:
Input: s = "carerac"
Output: trueConstraints:
1 <= s.length <= 5000s consists of only lowercase English letters.class Solution:
def canPermutePalindrome(self, s: str) -> bool:
count = 0
for i in range(128): # For all ASCII characters
if count > 1:
break
ct = 0
for j in range(len(s)):
if s[j] == chr(i): # Comparing with ASCII character
ct += 1
count += ct % 2
return count <= 1Where is the size of the input string
sand where is the number of unique characters ins
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
from collections import Counter
count = Counter(s)
odds = sum(val % 2 for val in count.values())
return odds <= 1Where is the size of the input string
sand where is the number of unique characters ins
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
map = [0] * 128
for ch in s:
map[ord(ch)] += 1
count = 0
for c in map:
if c % 2:
count += 1
return count <= 1Where is the size of the input string
sand where is the number of unique characters ins
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
map = [0] * 128
count = 0
for i in range(len(s)):
map[ord(s[i])] += 1
if map[ord(s[i])] % 2 == 0:
count -= 1
else:
count += 1
return count <= 1Where is the size of the input string
sand where is the number of unique characters ins
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
chars = set()
for c in s:
if c in chars:
chars.remove(c)
else:
chars.add(c)
return len(chars) <= 1Where is the size of the input string
sand where is the number of unique characters ins