-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ10453.java
More file actions
52 lines (42 loc) · 1.02 KB
/
BOJ10453.java
File metadata and controls
52 lines (42 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package quki.algorithm.string;
import java.util.ArrayList;
import java.util.Scanner;
public class BOJ10453 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
while (N-- > 0) {
String line[] = sc.nextLine().split(" ");
String A = line[0];
String B = line[1];
if (A.length() != B.length()) {
System.out.println(-1);
} else if (A.equals(B)) {
System.out.println(0);
} else {
ArrayList<Integer> aList = new ArrayList<>();
ArrayList<Integer> bList = new ArrayList<>();
for (int i = 0; i < A.length(); i++) {
char a = A.charAt(i);
char b = B.charAt(i);
if (a == 'a') {
aList.add(i);
}
if (b == 'a') {
bList.add(i);
}
}
if (aList.size() != bList.size()) {
System.out.println(-1);
} else {
int ans = 0;
for (int i = 0; i < aList.size(); i++) {
ans += Math.abs(aList.get(i) - bList.get(i));
}
System.out.println(ans);
}
}
}
}
}