Skip to content

Commit 6844c31

Browse files
Subsequence Hate
1 parent 1f37cfa commit 6844c31

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

1363/B/Main.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
7+
int t = sc.nextInt();
8+
for (int tc = 0; tc < t; ++tc) {
9+
String s = sc.next();
10+
11+
System.out.println(solve(s));
12+
}
13+
14+
sc.close();
15+
}
16+
17+
static int solve(String s) {
18+
int[] lefts = new int[2];
19+
int[] rights = new int[] { s.replaceAll("1", "").length(), s.replaceAll("0", "").length() };
20+
int result = Math.min(rights[0], rights[1]);
21+
for (char ch : s.toCharArray()) {
22+
int digit = ch - '0';
23+
++lefts[digit];
24+
--rights[digit];
25+
26+
result = Math.min(result, Math.min(lefts[0], lefts[1]) + Math.min(rights[0], rights[1]));
27+
}
28+
29+
return result;
30+
}
31+
}

0 commit comments

Comments
 (0)