본문 바로가기
Coding Test/Java Coding Test

[프로그래머스 / 자바] - 글자 지우기

by nam_ji 2024. 6. 16.

글자 지우기

문제

  • 문자열 my_string과 정수 배열 indices가 매개변수로 주어집니다.
  • my_string에서 indices의 원소에 해당하는 인덱스의 글자를 지우고
  • 이어 붙인 문자열을 출력하는 문제입니다.


테스트 (인텔리제이)

  • 세가지 해결 방법
    1. String을 통해 특정 문자 지운 후 더하기
      • my_string을 split 메서드를 이용하여 모든 문자를 분리하여 저장합니다. (str에 저장)
      • for문을 indices 배열의 크기만큼 순회하도록 하고
      • 그 안에서 str의 indices의 원소 값 위치의 원소를 빈 문자로 대체하여 저장합니다.
      • 다시 for문을 str의 크기만큼 순회하도록 하고
      • answer에 str의 문자를 하나씩 더해주면 됩니다. (빈 문자는 제외하고 더해집니다.)
    2. StringBuilder를 통해 특정 문자 지운 후 더하기
      • 위 방법과 전체적으로 동일하지만 answer에 문자를 더할 때 Stringbuilder는 append 메서드를 이용하여 더해줍니다.
    3. String 클래스의 replace 메서드 이용하기
      • my_string을 StringBuilder로 변환합니다. (answer)
      • for문을 indices의 크기만큼 순회하면서 answer를 StringBuilder의 setCharAt() 메서드를 이용하여 indices의 각 원소 값의 위치를 빈 값으로 대체합니다.
      • 마지막으로 answer를 toString메서드로 Stiring타입으로 변환하고 replace를 통해 빈 값을 없애줍니다.

 

public class 글자_지우기 {
  public static void main(String[] args) {
    /*
    문자열 my_string과 정수 배열 indices가 매개변수로 주어집니다.
    my_string에서 indices의 원소에 해당하는 인덱스의 글자를 지우고
    이어 붙인 문자열을 출력하는 문제입니다.

    입출력 예
    my_string	            indices	                      result
    "apporoograpemmemprs"	[1, 16, 6, 15, 0, 10, 11, 3]	"programmers"

    입출력 설명
    index	    0	1	2	3	4	5	6	7	8	9
    my_string	a	p	p	o	r	o	o	g	r	a
     */

    System.out.println("\n----------------String 더하기----------------");
    StringAnswer stringAnswer = new StringAnswer();
    stringAnswer.string();

    System.out.println("\n----------------StringBuilder 더하기----------------");
    StringBuilderAnswer stringBuilderAnswer = new StringBuilderAnswer();
    stringBuilderAnswer.stringBuilder();

    System.out.println("\n----------------String replace----------------");
    StringReplace stringReplace = new StringReplace();
    stringReplace.stringReplace();
  }
}

class StringAnswer {
  public void string() {
    String my_string = "apporoograpemmemprs";
    int[] indices = {1, 16, 6, 15, 0, 10, 11, 3};
    String[] str = my_string.split("");
    String answer = "";

    for (int i = 0; i < indices.length; i++) {
      str[indices[i]] = "";
    }

    for (String s : str) {
      answer += s;
    }

    System.out.println(answer);
  }
}

class StringBuilderAnswer {
  public void stringBuilder() {
    String my_string = "apporoograpemmemprs";
    int[] indices = {1, 16, 6, 15, 0, 10, 11, 3};
    String[] str = my_string.split("");
    StringBuilder answer = new StringBuilder();

    for (int i = 0; i < indices.length; i++) {
      str[indices[i]] = "";
    }

    for (String s : str) {
      answer.append(s);
    }

    System.out.println(answer);
  }
}

class StringReplace {
  public void stringReplace() {
    String my_string = "apporoograpemmemprs";
    int[] indices = {1, 16, 6, 15, 0, 10, 11, 3};
    StringBuilder answer = new StringBuilder(my_string);

    for (int i : indices) {
      answer.setCharAt(i, ' ');
    }

    System.out.println(answer.toString().replace(" ", ""));
  }
}

프로그래머스 코드

class Solution {
    public String solution(String my_string, int[] indices) {
        StringBuilder answer = new StringBuilder(my_string);
        
        for (int i : indices) {
            answer.setCharAt(i, ' ');
        }
        
        return answer.toString().replace(" ", "");
    }
}

// class Solution {
//     public StringBuilder solution(String my_string, int[] indices) {
//         String[] str = my_string.split("");
//         StringBuilder answer = new StringBuilder();
        
//         for (int i = 0; i < indices.length; i++) {
//             str[indices[i]] = "";
//         }
        
//         for (String s : str) {
//             answer.append(s);
//         }
        
//         return answer;
//     }
// }

// class Solution {
//     public String solution(String my_string, int[] indices) {
//         String[] str = my_string.split("");
//         String answer = "";
        
//         for (int i = 0; i < indices.length; i++) {
//             str[indices[i]] = "";
//         }
        
//         for (String s : str) {
//             answer += s;
//         }
        
//         return answer;
//     }
// }

String 더하기
StringBuilder 더하기
String replace