https://programmers.co.kr/learn/courses/30/lessons/60057

 

코딩테스트 연습 - 문자열 압축

데이터 처리 전문가가 되고 싶은 "어피치"는 문자열을 압축하는 방법에 대해 공부를 하고 있습니다. 최근에 대량의 데이터 처리를 위한 간단한 비손실 압축 방법에 대해 공부를 하고 있는데, 문

programmers.co.kr

 

카카오 블라인드 코딩 테스트

class Solution {
    public int solution(String s) {
        int len = s.length();
		int answer = len;

		for (int i = 1; i <= len / 2; i++) {
			String ss = s.substring(0, i);
			String ss2 = s.substring(0, len);
			String result = "";
			int cnt = 1;
			int cnt2 = len/i;

			for (int j = i; j < len; j += i) {
				if(len%i!=0) {
					if(cnt2==1)
						break;
					cnt2--;
				}
				if (ss.equals(s.substring(j, j + i))) {
					cnt += 1;
				} else {
					if (cnt == 1) {
						result += ss;
						ss = s.substring(j, j + i);
					} else {
						result += (cnt + ss);
						ss = s.substring(j, j + i);
						cnt = 1;
					}
				}
				ss2 = s.substring(j, len);
			}
			if (cnt == 1) {
				result += ss2;
			} else {
				result += (cnt + ss2);
			}
			if (answer > result.length()) {
				answer = result.length();
			}
		}
		return answer;
    }
}

+ Recent posts