Introduction
In this challenge, we will be compressing a string in a specific way to save space. The compressed string will have a shorter length than the original string, only if it saves space.
In this challenge, we will be compressing a string in a specific way to save space. The compressed string will have a shorter length than the original string, only if it saves space.
Given a string, compress it such that consecutive occurrences of the same character are replaced with that character followed by the number of occurrences. For example, the string 'AAABCCDDDD' would become 'A3BCCD4'. However, if the compressed string is not shorter than the original string, return the original string.
To solve this challenge, the following requirements must be met:
The following are examples of how this function can be used:
compress(None)
returns None
compress('')
returns ''
compress('AABBCC')
returns 'AABBCC'
compress('AAABCCDDDD')
returns 'A3BCCD4'
In summary, this challenge requires us to compress a string in a specific way to save space. We must ensure that the compressed string is shorter than the original string, otherwise we return the original string. The requirements for this challenge include using ASCII strings, being case sensitive, allowing additional data structures, and assuming the string fits in memory.