-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added a function that checks if given string can be rearranged to form a palindrome. #2450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cclauss
merged 6 commits into
TheAlgorithms:master
from
susmith98:checkIfStringCanBeConvertedToPalindrome
Sep 19, 2020
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a78facd
Added check_if_string_can_be_rearranged_as_palindrome function.
9a31de4
Added counter implementation and benchmark function.
a034156
flake changes
0b91109
Update and rename check_if_string_can_be_converted_to_palindrome.py t…
cclauss cd54a95
Update can_string_be_rearranged_as_palindrome.py
cclauss ea45a39
#
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Created by susmith98 | ||
|
||
# Problem Description: | ||
# Check if characters of the given string can be rearranged to form a palindrome. | ||
|
||
def check_if_string_can_be_rearranged_as_palindrome(input_str: str = "",) -> bool: | ||
""" | ||
A Palindrome is a String that reads the same forward as it does backwards. | ||
Examples of Palindromes mom, dad, malayalam | ||
>>> check_if_string_can_be_rearranged_as_palindrome("Momo") | ||
True | ||
>>> check_if_string_can_be_rearranged_as_palindrome("Mother") | ||
False | ||
>>> check_if_string_can_be_rearranged_as_palindrome("Father") | ||
False | ||
""" | ||
if len(input_str) == 0: | ||
return True | ||
lower_case_input_str = input_str.lower() | ||
# character_freq_dict: Stores the frequency of every character in the input string | ||
character_freq_dict = {} | ||
|
||
for character in lower_case_input_str: | ||
character_freq_dict[character] = character_freq_dict.get(character, 0) + 1 | ||
""" | ||
Above line of code is equivalent to: | ||
1) Getting the frequency of current character till previous index | ||
>>> character_freq = character_freq_dict.get(character, 0) | ||
2) Incrementing the frequency of current character by 1 | ||
>>> character_freq = character_freq + 1 | ||
3) Updating the frequency of current character | ||
>>> character_freq_dict[character] = character_freq | ||
""" | ||
""" | ||
OBSERVATIONS: | ||
Even length palindrome | ||
-> Every character appears even no.of times. | ||
Odd length palindrome | ||
-> Every character appears even no.of times except for one character. | ||
LOGIC: | ||
Step 1: We'll count number of characters that appear odd number of times i.e oddChar | ||
Step 2:If we find more than 1 character that appears odd number of times, | ||
It is not possible to rearrange as a palindrome | ||
""" | ||
oddChar = 0 | ||
|
||
for character_count in character_freq_dict.values(): | ||
if character_count % 2 == 1: | ||
oddChar = oddChar + 1 | ||
if oddChar > 1: | ||
return False | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
check_str = input( | ||
"Enter string to determine if it can be rearranged as a palindrome or not: " | ||
).strip() | ||
status = check_if_string_can_be_rearranged_as_palindrome(check_str) | ||
print(f"{check_str} can {'' if status else 'not '}be rearranged as a palindrome") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.