#C8293. Minimum Operations to Convert to All 'a's
Minimum Operations to Convert to All 'a's
Minimum Operations to Convert to All 'a's
You are given a string S consisting only of the characters a
and b
. You can perform the following operation any number of times:
- Choose a contiguous segment of one or more
b
's and flip all of them toa
's.
Your task is to determine the minimum number of operations required to convert the entire string to only a
's.
Formally, if we denote the string by \(S = s_1s_2 \ldots s_n\), one operation allows you to choose indices \(i\) and \(j\) (with \(1 \le i \le j \le n\)) such that for every \(k\) in \([i, j]\), \(s_k = b\), and then change every \(s_k\) in that range to a
. The goal is to achieve \(S = a_1a_2 \ldots a_n\) with as few operations as possible.
inputFormat
The input is read from stdin and has the following format:
T S₁ S₂ ⋮ Sₜ
where:
T
is a positive integer representing the number of test cases.- Each
Sᵢ
is a string consisting only of the charactersa
andb
.
outputFormat
For each test case, output a single line to stdout containing one integer: the minimum number of operations needed to convert the string into a string of a
's.
3
ababb
bbaa
aaaa
2
1
0
</p>