interactive-coding-challenges icon indicating copy to clipboard operation
interactive-coding-challenges copied to clipboard

string compression solutions don't match description

Open cristianmtr opened this issue 5 years ago • 2 comments

Hello

In the string compression challenge:

  • you say Only compress the string if it saves space.
  • but you have expected outputs like
        assert_equal(func('AAABCCDDDDE'), 'A3BC2D4E')
        assert_equal(func('BAAACCDDDD'), 'BA3C2D4')
        assert_equal(func('AAABAACCDDDD'), 'A3BA2C2D4')

The C2 doesn't save space, right? Shouldn't it be just CC ?

cristianmtr avatar Jun 08 '20 10:06 cristianmtr

What could we do here then to save space? I'd really like to know.

amitShindeGit avatar Oct 02 '20 05:10 amitShindeGit

Alphabets count should be taken only when they are recurring more than 2 times. Let's say we have string AAABCCD, in this string A occurs thrice then it will be A3, B and D are single so they will be taken as it is. But in case of C, count should be taken if it is ocurring more than twice. In this case it should be CC but when it is more than 2 suppose CCC then the count should be taken. We can do this by below method.

` s = "AAABCCDDDDE" output = '' hashMap = {} for i in range(0, len(s)): if s[i] not in hashMap: hashMap[s[i]] = 1 else: hashMap[s[i]] += 1

for key, value in hashMap.items(): if value > 2: output += str(key)+str(value) else: output += str(key)*value

print(output) `

adarsh-tyagi avatar Mar 10 '21 16:03 adarsh-tyagi