Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/jihye/thisiscodingtest/part03/Q03_reverseString/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package jihye.thisiscodingtest.part03.Q03_reverseString;

import java.util.Scanner;

/*
연속된 하나 이상의 숫자를 잡고 모두 뒤집는다.
*/
public class Solution {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String S = sc.next();

int countOne = 0;
//1로 바꿀때 몇번을 뒤집어야 하는지
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) == '0') {
countOne++;
while (i < S.length() && S.charAt(i) == '0') {
i++;
}
}
}

int countZero = 0;
//0으로 바꿀때 몇번을 뒤집어야하는지
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) == '1') {
countZero++;
while (i < S.length() && S.charAt(i) == '1') {
i++;
}
}
}

System.out.println(Math.min(countOne, countZero));
}

}