Python Coding Practice

Solve Basic String Compression using Python Language

Solve Basic String Compression using Python to enhance your skills with python coding practice , master coding concepts, and prepare for interviews with practical exercises and detailed solutions.

Basic String Compression

Difficulty : Easy

Categories :

  • Strings

Given a string s containing only lowercase letters, compress it by replacing consecutive repeated characters with the character followed by the count of repetitions. If the compressed string is not smaller than the original string, return the original string.


Constraints:

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase English letters only

Examples:

Input: "aabbbcccc"
Output: "a2b3c4"
Explanation: Two 'a's are replaced with 'a2', three 'b's with 'b3', and four 'c's with 'c4'.
Input: "abc"
Output: "abc"
Explanation: Since no character repeats, the compressed string (a1b1c1) would be longer than original string.
Input: "aaa"
Output: "a3"
Explanation: Three consecutive 'a's are compressed to 'a3'.

Input Format:

The input consists of a single line containing a JSON string.


Output Format:

Print a JSON string representing the compressed string if it's shorter than the original, otherwise print the original string.

Problem Solving

Input

What You'll Find Here

Interactive Exercises Practice coding with problems designed for beginners and experts.

Step-by-Step Solutions Understand every step of the solution process.

Real-World Scenarios Apply your skills to real-world problems and boost your confidence.

Choose from the following categories