Dataford
Interview Guides
Upgrade
All questions/Coding/Run-Length String Compression Choice

Run-Length String Compression Choice

Easy
Coding
Asked at 1 company1ArraysStringsHash Tables
Also asked at
Poshmark

Problem

Problem

At Dropbox, you are asked to implement a lightweight string compressor using run-length encoding. Given a string s, compress consecutive repeated characters so that each group becomes the character followed by its count, then return whichever is shorter: the compressed string or the original string.

Formal Specification

  • Input: a string s
  • Output: a string
  • Consecutive repeated characters are encoded as character + count
  • If the compressed result is not strictly shorter than the original string, return the original string

Examples

Example 1

  • Input: s = "aaabb"
  • Output: "a3b2"
  • Explanation: The groups are aaa and bb, so the compressed form is a3b2, which is shorter than aaabb.

Example 2

  • Input: s = "abc"
  • Output: "abc"
  • Explanation: The compressed form would be a1b1c1, which is longer than the original string.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain uppercase letters, lowercase letters, digits, spaces, or symbols
  • Compression is based only on consecutive characters
  • You must preserve character order exactly

Examples

Example 1
Inputs = "aaabb"Output"a3b2"WhyThere are two runs: `aaa` and `bb`. The compressed result `a3b2` has length 4, which is shorter than the original length 5.
Example 2
Inputs = "abc"Output"abc"WhyThe compressed form would be `a1b1c1`, which is longer than `abc`, so the original string is returned.
Example 3
Inputs = "aabcccccaaa"Output"a2b1c5a3"WhyThe runs are `aa`, `b`, `ccccc`, and `aaa`. The compressed string is shorter than the original.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain any printable characters
  • Compression uses consecutive character runs only
  • Return the original string if the compressed version is not strictly shorter

Function Signature

def compress_string(s):

Problem

Problem

At Dropbox, you are asked to implement a lightweight string compressor using run-length encoding. Given a string s, compress consecutive repeated characters so that each group becomes the character followed by its count, then return whichever is shorter: the compressed string or the original string.

Formal Specification

  • Input: a string s
  • Output: a string
  • Consecutive repeated characters are encoded as character + count
  • If the compressed result is not strictly shorter than the original string, return the original string

Examples

Example 1

  • Input: s = "aaabb"
  • Output: "a3b2"
  • Explanation: The groups are aaa and bb, so the compressed form is a3b2, which is shorter than aaabb.

Example 2

  • Input: s = "abc"
  • Output: "abc"
  • Explanation: The compressed form would be a1b1c1, which is longer than the original string.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain uppercase letters, lowercase letters, digits, spaces, or symbols
  • Compression is based only on consecutive characters
  • You must preserve character order exactly

Examples

Example 1
Inputs = "aaabb"Output"a3b2"WhyThere are two runs: `aaa` and `bb`. The compressed result `a3b2` has length 4, which is shorter than the original length 5.
Example 2
Inputs = "abc"Output"abc"WhyThe compressed form would be `a1b1c1`, which is longer than `abc`, so the original string is returned.
Example 3
Inputs = "aabcccccaaa"Output"a2b1c5a3"WhyThe runs are `aa`, `b`, `ccccc`, and `aaa`. The compressed string is shorter than the original.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain any printable characters
  • Compression uses consecutive character runs only
  • Return the original string if the compressed version is not strictly shorter

Function Signature

def compress_string(s):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
Opera SolutionsLongest Unique Character SubstringMediumMeta ITLongest Unique Character SubstringMediumQuantum HealthLongest Unique Substring LengthMedium
Next question