class Questions::Issue20260309
Question
Given a string
sconsisting only of ‘a’ and ‘b’, you may swap adjacent characters any number of times. Return the minimum number of adjacent swaps needed to transformsinto an alternating string, either “ababab…” or “bababa…”, or return-1if it’s impossible.
Examples
Public Instance Methods
Source
# File questions/20260309.rb, line 22 def setup @methods = Answers::Issue20260309.instance_methods(false) @examples = [ { string: "aabb", expected: 1, }, { string: "aaaabbbb", expected: 6, }, { string: "aaab", expected: -1, }, # Extra examples by izkreny { string: "ba", expected: 0, }, { string: "baa", expected: 1, }, { string: "bbbaa", expected: 3, }, ] end
Tests
Public Instance Methods
Source
# File questions/20260309.rb, line 55 def test_answers_with_examples @methods.each do |method| @examples.each do |example| actual = public_send(method, example[:string]) assert_equal example[:expected], actual, "Answer #{method} is not correct for '#{example[:string]}' string" refute_same example[:string], actual, "Answer #{method} is messing with the string" end end end