naren_paste

string_Compression

Aug 5th, 2024
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | Source Code | 0 0
  1. input_string = input("Enter a string: ")
  2. compressed_string = ""
  3. count = 1
  4.  
  5. for i in range(len(input_string)):
  6.     if i + 1 < len(input_string) and input_string[i] == input_string[i + 1]:
  7.         count += 1
  8.     else:
  9.         compressed_string += input_string[i] + str(count)
  10.         count = 1
  11.        
  12. if len(compressed_string) >= len(input_string):
  13.     print("Compressed string:", input_string)
  14. else:
  15.     print("Compressed string:", compressed_string)
Advertisement
Add Comment
Please, Sign In to add comment