• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

혹시 이 방법으로 풀어도 괜찮을까요?

22.05.09 06:18 작성 조회수 187

0

- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 1:1 문의하기를 이용해주세요.
 
import java.util.*;
public class 문자열압축 {
    public static String solution(String str){
        String answer = "";
        char temp=' ';
        int num = 1;
        str+=" ";
        for(int i=0;i<str.length();i++){
            if(temp==str.charAt(i)){
                num++;
            }else{  
                answer+=temp+Integer.toString(num);
                num=1;
                
            }
            temp=str.charAt(i);
        }
        answer=answer.replaceAll("1","").trim();
        return answer;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        System.out.println(solution(str));
    }
}

replaceAll, trim을 사용하여 출력을 받았고 이전에 저장해준 값을 가져와서 쓰는 방식으로 구현하였습니다. 

답변 1

답변을 작성해보세요.

0

안녕하세요^^

네. 잘하신 코드입니다. 문자열 문제는 어떻게 하든 답을 내면 됩니다.