Skip to content

Commit cbfa37e

Browse files
committed
Merge branch 'master' of bitbucket.org:osaatcioglu/sumeyya_ctci
2 parents 001b990 + bdd4471 commit cbfa37e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

string_compression.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def compress(string):
2+
result = str()
3+
count = 1
4+
prev_s = string[0]
5+
string = string + " "
6+
for s in string[1:]:
7+
if s == prev_s:
8+
count = count + 1
9+
else:
10+
result = result + prev_s + str(count)
11+
count = 1
12+
prev_s = s
13+
return result if len(result) < len(string) else string
14+
15+
def main():
16+
string = "aaaaaaabca"
17+
ip = compress(string)
18+
print(ip)
19+
20+
if __name__ == "__main__":
21+
main()

0 commit comments

Comments
 (0)