Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Alphabet Type and Frequency Encoding

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Alphabet Type and Frequency Encoding. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

StubHub processes compact text labels for event listings. Implement run-length encoding for consecutive alphabetic characters, returning the encoded string only when it is strictly shorter than the original.

Formal Specification

Given a string s containing uppercase and lowercase English letters, encode each maximal consecutive run as the character followed by its count when the count is greater than one. A run of length one is represented by the character alone. If the encoded string is not shorter than s, return s unchanged. Preserve character case and run order.

Implement compress_listing_text(s) and return a string.

Constraints

  • 0 <= len(s) <= 100000
  • s contains only uppercase and lowercase English letters
  • Preserve the case and order of all characters
  • Return the original string when the encoded string is not strictly shorter

Function Signature

def compress_listing_text(s):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output