Hack Frontend Community

String Compression

OzonSber
Write a function compressString(str) that takes a string str and returns its compressed version. Conditions:
  • The compressed version of the string should contain the characters of the original string and the number of their consecutive repetitions.
  • If a character appears once, it is included in the string without a number.

Examples:

Input 1: "AAB"
Output 1: "A2B"
Input 2: "ABC"
Output 2: "ABC"
Run your code to see results.