Official

C - 文字列圧縮 / String Compression Editorial by admin Editorial for printing

Claude 4.5 Opus

Overview

This problem asks you to count consecutive identical characters in a string and compress sections that repeat \(K\) or more times into the format “character+count”.

Analysis

Understanding the Problem

First, let’s verify the behavior with a concrete example.

  • Input: aaabbc, \(K = 2\)
  • aaa → 3 consecutive ≥ 2, so compress to a3
  • bb → 2 consecutive ≥ 2, so compress to b2
  • c → 1 consecutive < 2, so keep as c
  • Output: a3b2c

Key Insights

  1. We need to group consecutive identical characters
  2. For each group, branch the processing based on whether the consecutive count is \(K\) or more
  3. This can be solved with a single scan of the string (\(O(N)\))

Comparison with Naive Approach

This problem is relatively simple, and the naive approach of “scanning from the beginning” directly becomes the optimal solution.

However, since repeated string concatenation can be slow, it’s more efficient to store results in a list and join them all at the end.

Algorithm

  1. Initialization: Prepare a list result to store the output, and start from position \(i = 0\)

  2. Detecting Consecutive Sections: Count consecutive identical characters starting from position \(i\)

    • Record the current character current_char
    • Increment the counter while the next character is the same
  3. Compression Decision:

    • If consecutive count count ≥ \(K\): Add in (character)(count) format
    • If consecutive count count < \(K\): Add the original characters as-is
  4. Position Update: Advance \(i\) by count

  5. Repeat: Repeat steps 2-4 until the end of the string

  6. Output Result: Join the list elements and output

Example Walkthrough

Input: aaabbc, \(K = 2\)

\(i\) Target count Decision Added to result
0 aaa 3 3 ≥ 2 a3
3 bb 2 2 ≥ 2 b2
5 c 1 1 < 2 c

Final result: a3b2c

Complexity

  • Time Complexity: \(O(|S|)\)

    • Only one scan through the string is needed
    • Each character is processed exactly once
  • Space Complexity: \(O(|S|)\)

    • Size of the list storing the result
    • In the worst case (no compression at all), it equals the input length

Implementation Notes

  1. Adding to List and Final Join:

    • In Python, repeated string concatenation is slow, so it’s more efficient to append to a list and join with "".join() at the end
  2. Method for Counting Consecutive Characters:

    • while i + count < n and s[i + count] == current_char prevents out-of-bounds access while counting
  3. Using f-strings:

    • f"{current_char}{count}" concisely creates the “character+count” format
  4. Handling Cases Below K:

    • current_char * count restores the original consecutive string (e.g., c * 1 = "c", b * 2 = "bb")

Source Code

def compress_string(s, k):
    if not s:
        return ""
    
    result = []
    i = 0
    n = len(s)
    
    while i < n:
        current_char = s[i]
        count = 1
        
        # Count consecutive identical characters
        while i + count < n and s[i + count] == current_char:
            count += 1
        
        # Compress if K or more times, otherwise keep as-is
        if count >= k:
            result.append(f"{current_char}{count}")
        else:
            result.append(current_char * count)
        
        i += count
    
    return "".join(result)


# Input
S = input()
K = int(input())

# Compress and output
print(compress_string(S, K))

This editorial was generated by claude4.5opus.

posted:
last update: