Open cristianmtr opened 4 years ago
What could we do here then to save space? I'd really like to know.
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) `
Hello
In the string compression challenge:
Only compress the string if it saves space.
The
C2
doesn't save space, right? Shouldn't it be justCC
?