A string of lowercase English letters is given. For each run of the same letter repeated in a row, write the letter followed by the length of the run, and print everything on one line. For example "aaabcc" is 3 a's, 1 b and 2 c's, so the answer is "a3b1c2". Write 1 even for a single letter, and equal letters that are not adjacent form separate runs ("aba" → "a1b1a1").
Input
length 1 to 5,000, lowercase letters only
Output
the compressed string
Example 1 · stdin
aaabcc
stdout
a3b1c2
Example 2 · stdin
zzzzzzzzzzzzy
stdout
z12y1
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
The most common mistake is forgetting the last run. If you write "emit the previous run when the letter changes", you must emit the leftover run once more after the loop. Finding the end of each run with an inner loop and writing it right away avoids the problem. Collecting the result: Python gathers a list and "".joins it, Java and C# use StringBuilder, C++ += on std::string, C prints directly with printf("%c%d"), JS/TS join an array with join(""), Craykon uses out = out + s[i] + (j - i). In Java and C#, repeated + on a string builds a new string every time, so it slows down as the result grows. And in C, writing a single character '0' + count breaks runs longer than 9.
Pythonmain.py · 10 lines
s = input()parts = []i = 0while i < len(s): j = i while j < len(s) and s[j] == s[i]: j += 1 parts.append(s[i] + str(j - i)) i = jprint("".join(parts))
JavaScriptmain.js · 10 lines
const s = require("fs").readFileSync(0, "utf8").trim();const parts = [];let i = 0;while (i < s.length) { let j = i; while (j < s.length && s[j] === s[i]) j++; parts.push(s[i] + (j - i)); i = j;}console.log(parts.join(""));
TypeScriptmain.ts · 11 lines
import { readFileSync } from "node:fs";const s: string = readFileSync(0, "utf8").trim();const parts: string[] = [];let i = 0;while (i < s.length) { let j = i; while (j < s.length && s[j] === s[i]) j++; parts.push(s[i] + (j - i)); i = j;}console.log(parts.join(""));
JavaMain.java · 18 lines
import java.io.BufferedReader;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine().trim(); StringBuilder out = new StringBuilder(); int i = 0; while (i < s.length()) { int j = i; while (j < s.length() && s.charAt(j) == s.charAt(i)) j++; out.append(s.charAt(i)).append(j - i); i = j; } System.out.println(out); }}
C#Main.cs · 20 lines
using System;using System.Text;class Program{ static void Main() { string s = Console.ReadLine().Trim(); var sb = new StringBuilder(); int i = 0; while (i < s.Length) { int j = i; while (j < s.Length && s[j] == s[i]) j++; sb.Append(s[i]).Append(j - i); i = j; } Console.WriteLine(sb.ToString()); }}
C++main.cpp · 16 lines
#include <iostream>#include <string>int main() { std::string s, out; std::cin >> s; size_t i = 0; while (i < s.size()) { size_t j = i; while (j < s.size() && s[j] == s[i]) j++; out += s[i]; out += std::to_string(j - i); i = j; } std::cout << out << "\n";}
Cmain.c · 16 lines
#include <stdio.h>static char s[5005];int main(void) { if (scanf("%5000s", s) != 1) return 0; int i = 0; while (s[i]) { int j = i; while (s[j] == s[i]) j++; printf("%c%d", s[i], j - i); i = j; } printf("\n"); return 0;}
Craykonmain.cray · 14 lines
s = Console.ReadLine().Trim();out = "";i = 0;while (i < s.Length){ j = i; while (j < s.Length && s[j] == s[i]) { j++; } out = out + s[i] + (j - i); i = j;}Console.WriteLine(out);