이제 문자열을 압축하고, 압축된 문자열의 길이를 반환하는 compress() 메서드를 선언합니다.

    private int compress(String source, int length) {
        // 압축한 문자열의 길이 반환
    }

     

    2. 설정된 길이만큼 문자열을 잘라 낸 token의 배열 생성

    그런데 압축하려면 우선 length 길이씩 문자열을 잘라야 합니다. 다음과 같이 문자열을 length 길이씩 잘라 리스트에 담아 주는 split() 메서드를 선언합니다.

    private List<String> split(String source, int length) {
        List<String> tokens = new ArrayList<>();
        // source를 length만큼씩 잘라 tokens 리스트에 추가
        return tokens;
    }

    문자열을 자르는 시작 인덱스는 0부터 시작하여 length만큼씩 증가합니다. 따라서 다음 반복문으로 모든 startIndex를 순회할 수 있습니다.

    for (int startIndex = 0; startIndex < source.length(); startIndex += length) {
        // 문자열을 startIndex부터 잘라 tokens 리스트에 추가
    }
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.