#K44397. Minimum Steps to Empty the String

    ID: 27522 Type: Default 1000ms 256MiB

Minimum Steps to Empty the String

Minimum Steps to Empty the String

You are given a string S consisting only of the characters 'a', 'b', and 'c'. In one step, you can remove either a single character from the string, or you can remove a substring of three consecutive identical characters (i.e., "aaa", "bbb", or "ccc"). Your task is to determine the minimum number of steps required to make the string empty.

The key observation is that whenever you encounter three consecutive characters that are the same, it is optimal to remove them together, as this counts as one step rather than three individual removals. If no such triplet is available at a position, you remove a single character and proceed. The problem requires a simple greedy solution by scanning the string from left to right.

The solution must read input from standard input (stdin) and print the result to standard output (stdout).


(\textbf{Input Format:}) The input consists of a single line containing the string S.

(\textbf{Output Format:}) Output a single integer, which is the minimum number of steps required to empty the string.

inputFormat

The input is read from stdin. It consists of a single line containing the string S (only the letters 'a', 'b', and 'c'). Note that the string may be empty.

outputFormat

Print a single integer to stdout, representing the minimum number of steps to make the string empty.## sample

abccba
6