1

You have a key-value pair given:

"Adsddf" : ?

"ds?xsc" : :

"csdcs:" : #

...n items.

The goal is to decompress a string : Example : String: "ac3d:cs?" will translate to : ac3ddsAdsddfxsccsAdsddf

Basically in place substitution for every character.

What is the best algorithm to achieve this?

1 Answer 1

2

Best is somewhat nebulous, but here is a possible solution.

Pseudo code

# reverse lookup
rev = {}
for key, value in items
  rev[value] =  key

result = ""

decode(input)
  for letter in input
    if letter in rev
      decode(rev[letter])
    else
      result.append(letter)

Just be careful that there are no cycles ...

Sign up to request clarification or add additional context in comments.

2 Comments

Missing an else there, but yeah. This is the cleanest approach, and that’s a good default assumption for ‘best’ (particularly since any sane algorithm will have the same time complexity).
@Sneftel oh yes, I missed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.