You are given a string containing characters A and B only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you are allowed to delete zero or more characters in the string.
Your task is to find the minimum number of required deletions.
Example
Remove an A at positions 0 and 3 to make s = ABAB in 2 deletions.
Function Description
Complete the alternatingCharacters function in the editor below.
alternatingCharacters has the following parameter(s):
Returns
Input Format
The first line contains an integer q, the number of queries.
The next q lines each contain a string s to analyze.
Each string s will consist only of characters A and B.
단순하게 문자열에서 앞뒤로 비교해서 같은 경우만 카운트해주었다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
private static int alternatingCharacters(String s) {
int count = 0;
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == s.charAt(i + 1)) {
count++;
}
}
return count;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
String s = br.readLine();
System.out.println(alternatingCharacters(s));
}
br.close();
}
}