Compress String Using Python

AlgorithmAlgorithmBeginner
Practice Now

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

Introduction

In this Python challenge, we will be compressing a string by replacing repeated characters with a count of the repetition. The compressed string will only be returned if it saves space.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) 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/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/arrays_strings -.-> lab-268801{{"`Compress String Using Python`"}} python/variables_data_types -.-> lab-268801{{"`Compress String Using Python`"}} python/strings -.-> lab-268801{{"`Compress String Using Python`"}} python/type_conversion -.-> lab-268801{{"`Compress String Using Python`"}} python/conditional_statements -.-> lab-268801{{"`Compress String Using Python`"}} python/for_loops -.-> lab-268801{{"`Compress String Using Python`"}} python/lists -.-> lab-268801{{"`Compress String Using Python`"}} python/tuples -.-> lab-268801{{"`Compress String Using Python`"}} python/function_definition -.-> lab-268801{{"`Compress String Using Python`"}} python/standard_libraries -.-> lab-268801{{"`Compress String Using Python`"}} python/classes_objects -.-> lab-268801{{"`Compress String Using Python`"}} python/encapsulation -.-> lab-268801{{"`Compress String Using Python`"}} python/build_in_functions -.-> lab-268801{{"`Compress String Using Python`"}} end

Compress

Problem

Given a string, compress it such that 'AAABCCDDDD' becomes 'A3BC2D4'. The compressed string should only be returned if it saves space. If the compressed string is longer than the original string, return the original string. The string is case sensitive and can be assumed to be ASCII. Additional data structures can be used and it can be assumed that the string fits in memory.

Requirements

The following requirements must be met:

  • The string is ASCII.
  • The string is case sensitive.
  • Additional data structures can be used.
  • The string fits in memory.

Example Usage

The following examples demonstrate the expected input and output of the function:

  • None -> None
  • '' -> ''
  • 'AABBCC' -> 'AABBCC'
  • 'AAABCCDDDD' -> 'A3BC2D4'

Summary

In this Python challenge, we have learned how to compress a string by replacing repeated characters with a count of the repetition. We have also learned that the compressed string should only be returned if it saves space and that the string is case sensitive, ASCII, and fits in memory.

Other Algorithm Tutorials you may like