Remove same number of duplicate characters for each character in a string

时间:2016-04-07 10:26:28

标签: ruby string duplicates

How would it be possible to remove the same number of duplicates for each character in a string until only one instance is left of one of the characters stopping the process of removing duplicates from the remaining characters?

If I have a string:

string = "aaaabbbxxxxx44444oooooo9999999"

You can see that the character b is the character with the least number of duplicates (i.e. there's b followed by two more bs) so, if we remove 2 duplicates from each character set, we'd be left with the following without losing any characters used in the original string but minimising the number of duplicates for each character:

string = "aabxxx444oooo99999"

Also lets assume our string contains no white space and if it is jumbled:

string_b = "aabb4keekkk447abae777err99r9"

You can sort it first:

string_b = stringb.chars.sort.join
#=> string_b = "4447777999aaaabbbeeeekkkkrrr"

Before applying your reduce duplicates method:

string_b = "4779aabeekkr"

2 个答案:

答案 0 :(得分:2)

If you don't require the original order or chars, then you can do:

string_b = "aabb4keekkk447abae777err99r9"
h = string_b.chars.group_by { |c| c }.map { |c, a| [c, a.size] }.to_h
#=> {"a"=>4, "b"=>3, "4"=>3, "k"=>4, "e"=>4, "7"=>4, "r"=>3, "9"=>3}
# #to_h is optional here

n = h.values.min - 1
#=> 3
# use map(&:last) instead of #values if not using #to_h previously

h.map { |k, v| k * (v - n) }.sort.join
#=> "4779aabeekkr"

答案 1 :(得分:1)

你应该把它放在一个方法中。

def convert str
    return str if str.empty?
    letter_array = str.chars.group_by {|x| x}.values
    drop_size = letter_array.map(&:size).min - 1
    letter_array.map {|x| x.drop drop_size}.join
end

在irb console中运行

2.2.1 :230 > convert 'aaaabbbxxxxx44444oooooo9999999'
=> "aabxxx444oooo99999"
2.2.1 :231 > convert ''
=> ""
2.2.1 :232 > convert 'abc'
=> "abc"
2.2.1 :233 > convert 'abcabcddd'
=> "abcdd"
2.2.1 :234 > convert "    "
=> " "

您希望使用各种输入(例如空字符串)进行测试,以确保其有效。