Programmers/[Java] Lv. 0
Programmers [Java] - Lv. 0 : 글자 지우기
Jelly-fish
2023. 12. 6. 11:18
class Solution
{
public String solution(String my_string, int[] indices) {
String result;
StringBuilder strBui = new StringBuilder(my_string);
// ─────────────────────────────────────────────────────────
// 0123456
// "abcdefg" int[] indices = {1, 5, 3, 0, 6};
// ─────────────────────────────────────────────────────────
/*
* 0 1 2 3 4 5
* ('b') 1번 문자 삭제 → a c d e f g
* --------- → 삭제한 인덱스의 뒷 인덱스들이 1씩 줄어든다.
* (실제 삭제 index : 1)
* ____________________________________________________________________________
* 0 1 2 3 4
* ('f') 5번 문자 삭제 → a c d e g
* -- → 삭제한 인덱스의 뒷 인덱스들이 1씩 줄어든다.
*
* (실제 삭제 index : 4)
* ____________________________________________________________________________
* 0 1 2 3
* ('d') 3번 문자 삭제 → a c e g
* ---- → 삭제한 인덱스의 뒷 인덱스들이 1씩 줄어든다.
*
* (실제 삭제 index : 2)
* ____________________________________________________________________________
* 0 1 2
* ('a') 0번 문자 삭제 → c e g
* ------ → 삭제한 인덱스의 뒷 인덱스들이 1씩 줄어든다.
*
* (실제 삭제 index : 0)
*
* ____________________________________________________________________________
* 0 1
* ('g') 6번 문자 삭제 → c e
* --- → g가 맨 마지막 문자였으므로 인덱스 변동이 없다.
*
* (실제 삭제 index : 2)
*
*
* ★ [ 총 정 리 !!!
* ]============================================================================
* ======
* // 문자를 하나씩 삭제할 때마다 (strBui.deleteCharAt(indices[i]))
* // indices 배열의 요소 중 indices[i] 보다 큰 값의 요소들을 -1 해 주어야 한다!!
*
* // 『(i = 0) indices[i] = 1』
* // indices = {1, 16, 6, 15, 0, 10, 11, 3};
* // == == == == == ==
* // - 1 해 주어야 한다!
* // * (1보다 큰 제거 인덱스를 1씩 줄여줘야 한다. 문자열 길이가 줄어들기 때문에.)
*
*
*/
// [위의 설명을 정리한 결과~~]▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥
for (int i = 0; i < indices.length; i++) {
strBui.deleteCharAt(indices[i]);
for (int j = 0; j < indices.length; j++) {
if (indices[i] < indices[j]) {
indices[j]--;
}
}
}
// ▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥▥
result = strBui.toString();
return result;
}
}