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

  • Home
  • SEARCH
  • 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 7057743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:01:36+00:00 2026-05-28T04:01:36+00:00

Given a run length encoded string, say A3B1C2D1E1, decode the string in-place. The answer

  • 0

Given a run length encoded string, say “A3B1C2D1E1”, decode the string in-place.
The answer for the encoded string is “AAABCCDE”. Assume that the encoded array is large enough to accommodate the decoded string, i.e. you may assume that the array size = MAX[length(encodedstirng),length(decodedstring)].

This does not seem trivial, since merely decoding A3 as ‘AAA’ will lead to over-writing ‘B’ of the original string.

Also, one cannot assume that the decoded string is always larger than the encoded string.
Eg: Encoded string – ‘A1B1’, Decoded string is ‘AB’. Any thoughts?

And it will always be a letter-digit pair, i.e. you will not be asked to converted 0515 to 0000055555

  • 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-05-28T04:01:37+00:00Added an answer on May 28, 2026 at 4:01 am

    If we don’t already know, we should scan through first, adding up the digits, in order to calculate the length of the decoded string.

    It will always be a letter-digit pair, hence you can delete the 1s from the string without any confusion.

    A3B1C2D1E1
    

    becomes

    A3BC2DE
    

    Here is some code, in C++, to remove the 1s from the string (O(n) complexity).

    // remove 1s
    int i = 0; // read from here
    int j = 0; // write to here
    while(i < str.length) {
        assert(j <= i); // optional check
        if(str[i] != '1') {
            str[j] = str[i];
            ++ j;
        }
        ++ i;
    }
    str.resize(j); // to discard the extra space now that we've got our shorter string
    

    Now, this string is guaranteed to be shorter than, or the same length as, the final decoded string. We can’t make that claim about the original string, but we can make it about this modified string.

    (An optional, trivial, step now is to replace every 2 with the previous letter. A3BCCDE, but we don’t need to do that).

    Now we can start working from the end. We have already calculated the length of the decoded string, and hence we know exactly where the final character will be. We can simply copy the characters from the end of our short string to their final location.

    During this copy process from right-to-left, if we come across a digit, we must make multiple copies of the letter that is just to the left of the digit. You might be worried that this might risk overwriting too much data. But we proved earlier that our encoded string, or any substring thereof, will never be longer than its corresponding decoded string; this means that there will always be enough space.

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

Sidebar

Related Questions

I am building a .NET application that given a connection string, at run time,
Given that both Ruby on Rails (RoR) and Grails can run on the JVM
Given Java's write once, run anywhere paradigm and the fact that the Java tutorials
Given a table or a temp table, I'd like to run a procedure that
When I run this: use feature ':5.10'; $x=1; given ($x) { when(1) { say
Given: A string dayCodes (i.e. MWF or MRFU ) that I need to split
I've got a multi-dimensional array (json encoded, so is that still the right terminology?)
I have run into an interesting problem lately: Lets say I have an array
I have a string array that is being filled dynamically.... I would like to
I have a function that I want to allow to run for a given

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.