forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3.java
33 lines (27 loc) Β· 965 Bytes
/
3.java
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
import java.util.*;
public class Main {
public static String str;
public static int count0 = 0; // μ λΆ 0μΌλ‘ λ°κΎΈλ κ²½μ°
public static int count1 = 0; // μ λΆ 1λ‘ λ°κΎΈλ κ²½μ°
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
str = sc.next();
// 첫 λ²μ§Έ μμμ λν΄μ μ²λ¦¬
if (str.charAt(0) == '1') {
count0 += 1;
}
else {
count1 += 1;
}
// λ λ²μ§Έ μμλΆν° λͺ¨λ μμλ₯Ό νμΈνλ©°
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) != str.charAt(i + 1)) {
// λ€μ μμμ 1λ‘ λ°λλ κ²½μ°
if (str.charAt(i + 1) == '1') count0 += 1;
// λ€μ μμμ 0μΌλ‘ λ°λλ κ²½μ°
else count1 += 1;
}
}
System.out.println(Math.min(count0, count1));
}
}