- 난이도: Lv1
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/140108
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/1/140108. 문자열 나누기
풀이 시간 : 32분
import java.util.*;
class Solution {
public int solution(String s) {
int answer = 1;
int count = 1;
char x = 0;
for (int i = 1; i < s.length(); i++) {
if (count == 0) {
answer++;
s.charAt(x) = s.charAt(i);
}
if (s.charAt(x) == s.charAt(i)) {
count++;
}
else {
count--;
}
}
return answer;
}
}
//before
char x = 0;
for (int i = 1; i < s.length(); i++) {
if (count == 0) {
answer++;
s.charAt(x) = s.charAt(i);
}
if (s.charAt(x) == s.charAt(i)) {
count++;
}
//after
char x = s.charAt(0);
for (int i = 1; i < s.length(); i++) {
if (count == 0) {
answer++;
x = s.charAt(i);
}
if (x == s.charAt(i)) {
count++;
}
else {
count--;
}
풀이 시간 : 45분(첫 풀이 시간 포함)
import java.util.*;
class Solution {
public int solution(String s) {
int answer = 1;
int count = 1;
char x = s.charAt(0);
for (int i = 1; i < s.length(); i++) {
if (count == 0) {
answer++;
x = s.charAt(i);
}
if (x == s.charAt(i)) {
count++;
}
else {
count--;
}
}
return answer;
}
}
class Solution {
public int solution(String s) {
int answer = 0;
while (!s.isEmpty()) {
int same = 0;
int diff = 0;
char x = s.charAt(0);
int i = 0;
while (i < s.length() && (same == 0 || same != diff)) {
if (s.charAt(i) == x) {
same++;
} else {
diff++;
}
i++;
}
s = s.substring(i);
answer++;
}
return answer;
}
}