Skip to content

Instantly share code, notes, and snippets.

@mailtodanish
Created December 20, 2021 13:19
Show Gist options
  • Save mailtodanish/514d108dbea6e41a3f8974a3825d64ee to your computer and use it in GitHub Desktop.
Save mailtodanish/514d108dbea6e41a3f8974a3825d64ee to your computer and use it in GitHub Desktop.
Problem Overview: String Compression
def compress(s):
compressed_string =""
length=0
ch=re.findall(r"(\w)\1*", s)
for z in ch:
m=re.search(r'{}*'.format(z), s)
length = len(m.group(0))
compressed_string ="{}{}{}".format(compressed_string,z,length)
s =s[length:]
return compressed_string.replace("1","")
assert compress("bbcceeee") == "b2c2e4"
assert compress("aaabbbcccaaa") == "a3b3c3a3"
assert compress("a") == "a"
@mailtodanish
Copy link
Author

import re

@mailtodanish
Copy link
Author

Using regular expression It will have minimum number of loop and no comparison statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment