Created
December 20, 2021 13:19
-
-
Save mailtodanish/514d108dbea6e41a3f8974a3825d64ee to your computer and use it in GitHub Desktop.
Problem Overview: String Compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using regular expression It will have minimum number of loop and no comparison statement.