String Compression Challenge

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") algorithm/BasicAlgorithmsGroup -.-> algorithm/arrays_strings("`Arrays Strings`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/AdvancedTopicsGroup -.-> python/generators("`Generators`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-268800{{"`String Compression Challenge`"}} algorithm/arrays_strings -.-> lab-268800{{"`String Compression Challenge`"}} python/variables_data_types -.-> lab-268800{{"`String Compression Challenge`"}} python/strings -.-> lab-268800{{"`String Compression Challenge`"}} python/type_conversion -.-> lab-268800{{"`String Compression Challenge`"}} python/conditional_statements -.-> lab-268800{{"`String Compression Challenge`"}} python/for_loops -.-> lab-268800{{"`String Compression Challenge`"}} python/lists -.-> lab-268800{{"`String Compression Challenge`"}} python/tuples -.-> lab-268800{{"`String Compression Challenge`"}} python/function_definition -.-> lab-268800{{"`String Compression Challenge`"}} python/standard_libraries -.-> lab-268800{{"`String Compression Challenge`"}} python/generators -.-> lab-268800{{"`String Compression Challenge`"}} python/data_collections -.-> lab-268800{{"`String Compression Challenge`"}} python/build_in_functions -.-> lab-268800{{"`String Compression Challenge`"}} end

Compress Alt

Problem

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.

Requirements

To solve this challenge, the following requirements must be met:

  • The string is assumed to be ASCII.
  • The compression is case sensitive.
  • Additional data structures can be used.
  • The string is assumed to fit in memory.

Example Usage

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'

Summary

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.

Other Algorithm Tutorials you may like