forked from DerailedPsyche/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar_to_palindro
45 lines (34 loc) · 1.04 KB
/
char_to_palindro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Python3 implementation to check if
# characters of a given string can
# be rearranged to form a palindrome
NO_OF_CHARS = 256
# function to check whether characters
# of a string can form a palindrome
def canFormPalindrome(st) :
# Create a count array and initialize
# all values as 0
count = [0] * (NO_OF_CHARS)
# For each character in input strings,
# increment count in the corresponding
# count array
for i in range( 0, len(st)) :
count[ord(st[i])] = count[ord(st[i])] + 1
# Count odd occurring characters
odd = 0
for i in range(0, NO_OF_CHARS ) :
if (count[i] & 1) :
odd = odd + 1
if (odd > 1) :
return False
# Return true if odd count is 0 or 1,
return True
# Driver program
if(canFormPalindrome("geeksforgeeks")) :
print("Yes")
else :
print("No")
if(canFormPalindrome("geeksogeeks")) :
print("Yes")
else :
print("No")
# This code is contributed by Nikita Tiwari