Created
June 11, 2018 16:44
-
-
Save neerajkumar/de37303829e68000517565bed2c21bab to your computer and use it in GitHub Desktop.
Hackerrank test - Counting the number ways an anagram possible - Ruby Solution
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
=begin | |
Two words are anagrams of one another if their letters can be rearranged to form the other word. | |
In this challenge, you will be given a string. You must split it into two contiguous substrings, then determine the minimum number of characters to change to make the two substrings into anagrams of one another. | |
For example, given the string 'abccde', you would break it into two parts: 'abc' and 'cde'. Note that all letters have been used, the substrings are contiguous and their lengths are equal. Now you can change 'a' and 'b' in the first substring to 'd' and 'e' to have 'dec' and 'cde' which are anagrams. Two changes were necessary. | |
Input Format | |
The first line will contain an integer, q, the number of test cases. | |
Each test case will contain a string s which will be concatenation of both the strings described above in the problem. | |
The given string will contain only characters in the range ascii[a-z]. | |
Constraints | |
* 1 <= q <= 100 | |
* 1 <= s <= 10^4 | |
* s consists only of characters in the range ascii[a-z]. | |
Output Format | |
For each test case, print an integer representing the minimum number of changes required to make an anagram. Print -1 if it is not possible. | |
Sample Input | |
6 | |
aaabbb | |
ab | |
abc | |
mnop | |
xyyx | |
xaxbbbxx | |
Sample Output | |
3 | |
1 | |
-1 | |
2 | |
0 | |
1 | |
Explanation | |
* Test Case #01: We split s into two strings S1='aaa' and S2='bbb'. We have to replace all three characters from the first string with 'b' to make the strings anagrams. | |
* Test Case #02: You have to replace 'a' with 'b', which will generate "bb". | |
* Test Case #03: It is not possible for two strings of unequal length to be anagrams of one another. | |
* Test Case #04: We have to replace both the characters of first string ("mn") to make it an anagram of the other one. | |
* Test Case #05: S1 and S2 are already anagrams of one another. | |
* Test Case #06: Here S1 = "xaxb" and S2 = "bbxx". You must replace 'a' from S1 with 'b' so that S1 = "xbxb". | |
=end | |
require 'byebug' | |
def anagram(s) | |
return -1 unless (s.length % 2 == 0) | |
string1, string2 = [s.slice(0..(s.length/2 - 1)), s.slice((s.length / 2)..s.length)] | |
hash = {} | |
string1.chars.each_with_index do |ch, index| | |
hash["#{ch}_#{index}"] = string2.include?(ch) | |
end | |
return 0 if hash.values.all? | |
return hash.values.find_all {|value| !value}.count | |
end | |
puts anagram('aaabbb') | |
puts anagram('ab') | |
puts anagram('abc') | |
puts anagram('mnop') | |
puts anagram('xyyx') | |
puts anagram('xaxbbbxx') | |
puts anagram('hhpddlnnsjfoyxpciioigvjqzfbpllssuj') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello,
please can you explain why it fails to work for "fdhlvosfpafhalll"
regards,
ruby_newby