https://www.acmicpc.net/problem/2138
밑에 풀이를 참고하여 작성하였다.
https://astrid-dm.tistory.com/429
< char Array를 String으로 바꾸기 >
char tmpChar[] = {'1', '0'};
String S = new String(tmpChar);
< String을 char Array로 바꾸기 >
String S = "abc";
char tmpChar[] = S.toCharArray();
import java.util.*;
import java.io.*;
public class Main {
static int N;
static int answer;
static String befS;
static String afS;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
answer = Integer.MAX_VALUE;
befS = br.readLine();
afS = br.readLine();
solve(0);
solve(1);
if(answer == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(answer);
}
}
public static void solve(int lightOn) {
char befArr[] = befS.toCharArray();
int count = 0;
if(lightOn == 0) {
befArr[0] = (befArr[0] == '0') ? '1' : '0';
befArr[1] = (befArr[1] == '0') ? '1' : '0';
count++;
}
for(int i = 1; i < N; i++) {
if(befArr[i - 1] != afS.charAt(i - 1)) {
befArr[i-1] = (befArr[i-1] == '0') ? '1' : '0';
befArr[i] = (befArr[i] == '0') ? '1' : '0';
if(i < N - 1) {
befArr[i+1] = (befArr[i+1] == '0') ? '1' : '0';
}
count++;
}
}
if(afS.equals(new String(befArr))) {
answer = Math.min(answer, count);
}
}
}