#C2165. Counting Valid 3-Character Substrings
Counting Valid 3-Character Substrings
Counting Valid 3-Character Substrings
You are given several test cases. For each test case, you must count the number of contiguous substrings of length 3 in the given string that satisfy one of the following conditions:
- The substring contains exactly two
a
's and oneb
. - The substring contains exactly two
b
's and onea
.
In other words, if we denote by \( S[i..i+2] \) a substring of length 3 of the given string, then it is valid if either:
\[ \text{count}_{a}(S[i..i+2]) = 2 \quad \text{and} \quad \text{count}_{b}(S[i..i+2]) = 1 \]or
\[ \text{count}_{b}(S[i..i+2]) = 2 \quad \text{and} \quad \text{count}_{a}(S[i..i+2]) = 1 \]Your task is to implement a solution that reads input from the standard input and writes the answers to the standard output.
inputFormat
The first line contains an integer \( T \) representing the number of test cases. Each of the following \( T \) lines contains a non-empty string \( S \) consisting only of lowercase letters.
outputFormat
For each test case, output a single integer on a new line representing the number of valid substrings of length 3 that meet the given criteria.
## sample3
ababab
aaa
babbbb
4
0
2
</p>