Skip to content

Commit 8c6c333

Browse files
authored
Merge pull request #92 from zzzyoonnn/main
[Solved] PRG_문자열 나누기
2 parents f1a8f0a + 9b37760 commit 8c6c333

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package Week2.공통.문자열나누기;
2+
3+
public class jiyoon {
4+
class Solution {
5+
public int solution(String s) {
6+
int answer = 0;
7+
char x = '0';
8+
int countX = 0; // x 등장 수
9+
int countO = 0; // x와 다른 문자 등장 수
10+
11+
for (int i = 0; i < s.length(); i++) {
12+
if (x == '0') x = s.charAt(i);
13+
14+
if (s.charAt(i) == x) { // x와 동일한 문자가 나왔다면,
15+
countX++;
16+
} else if (s.charAt(i) != x) { // x와 다른 문자가 나왔다면,
17+
countO++;
18+
}
19+
20+
if (countX == countO) {
21+
answer++; // 문자열 분할 개수 추가
22+
x = '0'; // 문자열 초기화
23+
}
24+
}
25+
26+
if ((countX - countO) != 0) answer++; // 더 이상 읽을 문자열이 없다면 문자열 분할 개수 추가
27+
return answer;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)