0

You're given an array of strings where each character in the string is lowercase. Each character and the length of each string is randomly generated. Encode the string such that: 1. The encoded output is a single string with minimum possible length 2. You should be able to decode the string later

I am thinking the mention of each character being lowercase is key here. Since there are only 26 lowercase characters, maybe we can encode them using 5 bits instead of 8 bits and then pack them. But I am not sure how to implement this bit packing while looping over the array of strings

5
  • 3
    The problem is under specified. What is the purpose of the encoding and how will the solution be evaluated? The trivial solution is to concatenate them separated by spaces. Commented Aug 5, 2019 at 21:13
  • Are you familiar with bitwise operations? programiz.com/c-programming/bitwise-operators Commented Aug 5, 2019 at 21:15
  • I think they want you to find a way to encode each two characters into one byte, i guess, you need to explain more Commented Aug 5, 2019 at 21:15
  • @ddyer: We want an encoded output which has a minimum possible length (as mentioned in condition 1). Just concatenating doesn't seem efficient enough. Commented Aug 5, 2019 at 21:37
  • Optimum compression requires much more information about the data to be compressed, and also about the representation of the underlying strings. If you assert that the original characters are random (and therefore non compressible) you could convert everything into a single base 27 bignum, then represent that as a stream of bytes. Commented Aug 6, 2019 at 6:18

1 Answer 1

1

For 26 characters and a separator you could use base32. Basically concatenate the strings with a delimiter and then do a base32 decode - should be easy to find code for that. Just do not use those characters that result in 4-5 zeros in binary so that you do not accidentally have the null terminator in the middle of your string.

For decoding you'll do base32 encode and then split the string at delimiters.

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

Comments

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.