def change(line) j = 0 count = 0 while true if j == line.length return count end if line[j] == "B" && line[j + 1] == "A" line[j] = "A" line[j + 1] = "B" count += 1 end j += 1 end end line = gets.chomp.split("") count = 0 line.length.times do count += change(line) end p count