https://programmers.co.kr/learn/courses/30/lessons/42883
class Solution {
public String solution(String number, int k) {
StringBuilder answer = new StringBuilder();
int idx = 0;
char max;
for (int i = 0; i < number.length() - k; i++) {
max = '0';
for (int j = idx; j <= k + i; j++) {
if (max < number.charAt(j)) {
max = number.charAt(j);
idx = j + 1;
}
}
answer.append(max);
}
return answer.toString();
}
}
가독성을 위해 변수에 의미를 부여하자!
class Solution {
public String solution(String number, int removeCnt) {
StringBuilder answer = new StringBuilder();
int numberIndex = 0;
char maxNumber;
int resultCnt = number.length() - removeCnt;
for (int resultIndex = 0; resultIndex < resultCnt; resultIndex++) {
maxNumber = '0';
for (int maxIndex = numberIndex; maxIndex <= removeCnt + resultIndex; maxIndex++) {
if (maxNumber < number.charAt(maxIndex)) {
maxNumber = number.charAt(maxIndex);
numberIndex = maxIndex + 1;
}
}
answer.append(maxNumber);
}
return answer.toString();
}
}