Last active
March 2, 2017 02:55
-
-
Save yovasx2/97a8df6983b528d9d62839ed17cb820b to your computer and use it in GitHub Desktop.
This file contains 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
#!usr/bin/ruby | |
# A palindrome is a string that reads the same left-to-right and right-to-left. For example, "Madam, I'm Adam" and "Poor Dan is in a droop" are both palindromes. Note that letter case and non-alphanumeric characters should be ignored when deciding whether a string is a palindrome or not. | |
# А string x is an anagram of another string y if you can obtain y by rearranging the letters of x. For example, "cinema" is an anagram of "iceman", and vice versa. Note that the string and its anagram must have the same length. By definition, the string is not considered as an anagram of itself. In anagrams, non-alphanumeric characters and letter case are important. For instance, "Oo" is not the same as "oO", making "Oo" an anagram of "oO" and vice versa. | |
# Given a message, your task is to determine whether there is an anagram of the message that is also a palindrome. | |
# Example | |
# For message = "abab", the output should be hasPalindromicAnagram(message) = true. | |
# Among the anagrams of "abab", there are two strings that are also palindromes ("abba" and "baab"), so the answer is true. | |
# For message = "bob", the output should be hasPalindromicAnagram(message) = false. | |
# The only rearrangement of the letters in the string "bob" that is a palindrome is the word itself, but this is not an anagram as defined above. Therefore, the answer is false. | |
# For message = "heh!", the output should be hasPalindromicAnagram(message) = true. | |
# "!heh", "h!eh" and "he!h" are all palindromes and all of them are anagrams of "heh!". Remember that according to the rules laid out above, non-alphanumeric characters are ignored in palindromes but need to be considered in anagrams. | |
# Input Format | |
# A string that will be checked to determine if there is an anagram of that string that is also a palindrome | |
# Constraints | |
# A string containing at least one alphanumeric character. | |
# 0 < message.length ≤ 20 | |
# Output Format | |
# You should print "true" or "false" if there is an anagram of the given string that is also a palindrome | |
# Sample Input 0 | |
# abab | |
# Sample Output 0 | |
# true | |
# Sample Input 1 | |
# bob | |
# Sample Output 1 | |
# false | |
# Sample Input 2 | |
# heh! | |
# Sample Output 2 | |
# true | |
def mix(str) | |
tmp = str | |
for i in 1..str.length-1 | |
mixed = swap(tmp, 0, i) | |
return mixed if mixed != str | |
tmp = str | |
end | |
tmp | |
end | |
def swap(str, i, j) | |
letters = str.split('') | |
tmp = letters[i] | |
letters[i] = letters[j] | |
letters[j] = tmp | |
letters.join('') | |
end | |
def has_reflection(str) | |
half = str.length/2 | |
first = str[0, half] | |
last = str[-half, str.length] | |
first == last.reverse | |
end | |
def is_palindromic(str) | |
has_unique = false | |
size = str.length | |
i = 0 | |
while i < size | |
coincidence = str[i+1, size-1].index(str[i]) | |
if coincidence | |
str = extract_letter_at(str, i) | |
str = extract_letter_at(str, coincidence) | |
elsif has_unique | |
return false | |
else | |
has_unique = true | |
str = extract_letter_at(str, i) | |
end | |
size = str.length | |
end | |
true | |
end | |
def extract_letter_at(str, i) | |
str = str.split('') | |
str[i] = '' | |
str.join('') | |
end | |
def has_palindromic_anagram(str) | |
return puts false if str.length < 2 | |
return puts false if str.length < 4 && has_reflection(str) | |
mixed = mix(str) | |
return puts false if mixed == str | |
clean = mixed.downcase.gsub(/\W/,'') | |
puts is_palindromic(clean) | |
end | |
message = gets | |
has_palindromic_anagram(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment