Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8756449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:05:12+00:00 2026-06-13T14:05:12+00:00

There is a frequently asked question in interviews about compressing a string. I’m not

  • 0

There is a frequently asked question in interviews about compressing a string.
I’m not looking for a code, I only need an efficient algorithm that solves the problem.

Given a string (e.g. aaabbccaaadd), compress it (3a2b2c3a2d).

My solution:

Travel on the string. Every time I see the same letter I count it.
I will output the letter and the counter when I see a different letter coming (and start over again).

Is there more efficient way to do this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:05:12+00:00Added an answer on June 13, 2026 at 2:05 pm

    That’s called running length encoding, and the algorithm you name is basically the best you’ll get. It takes O(1) auxiliary storage (save the last symbol seen, or equivalently inspect the upcoming element; also save a counter of how many identical symbols you’ve seen) and runs in O(n) time. As you need to inspect each symbol at least once to know the result, you can’t get better than O(n) time anyway. What’s more, it can also process streams one symbol at a time, and output one symbol at a time, so you actually only need O(1) RAM.

    You can pull a number of tricks to get the constant factors better, but the algorithm remains basically the same. Such tricks include:

    • If you stream to a slow destination (like disk or network), buffer. Extensively.
    • If you expect long runs of identical symbols, you may be able to vectorize the loop counting them, or at least make that loop tighter by moving out the other cases.
    • If applicable, tell your compiler not to worry about aliasing between input and output pointers.

    Such micro-optimizations may be moot if your data source is slow. For the level of optimization some of my points above address, even RAM can counts as slow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that this question gets asked frequently , but I am not coming
I know variants of this question have been asked frequently before (see here and
It might be a frequently asked question, however so far i couldn't find a
I realise a question like this is asked pretty frequently (I've probably read every
This question is a folo to a previous question I asked about how to
I have asked this question on superuser as well, but have not yet gotten
The same question was asked 1 year ago here . I'm looking for a
I frequently run into the following problem, I was wondering if there is any
I frequently work with the System.IO namespace. Is there a way to have that
I am trying to understand static analysis of Java bytecode/source-code. These terms frequently come

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.