238. String Compression (Length)

MediumStringString

Given a string s, compress it by replacing each group of consecutive repeated characters with the character followed by the group length (lengths of 1 are written as just the character). Return the length of the compressed string.

Input: A quoted string s.

Output: Integer — length of the compressed form.

Examples

Example 1
Input: "aabbbccccd"
Output: 7
Explanation: a2b3c4d -> length 7.
Example 2
Input: "abcde"
Output: 5
Explanation: No compression; counts of 1 omitted.
Example 3
Input: "aaaa"
Output: 2
Explanation: a4 -> length 2.

Constraints

Asked by

PaytmAmazonAppleBloombergMicrosoftAccenture
Solve this problem in the editor →