문제
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV19AcoKI9sCFAZN
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이
인덱스 0 부터 시작하여 비트의 숫자가 바뀌면 count를 +1 해주면 된다.
처음에 풀었을때는 직접 비트 배열을 만들었는데 그럴 필요 없이 비트 숫자가 바뀌면 count+1를 해주면 된다.
코드
import java.io.*;
class Solution {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int test_case = 1; test_case <= T; test_case++)
{
String str = br.readLine();
int count = 0;
char before = '0';
for(int i=0;i<str.length();i++) {
if(before != str.charAt(i)) {
count++;
before = str.charAt(i);
}
}
System.out.println("#"+test_case+" "+count);
}
}
}