Problem 228357 · easy · Level 02 Linear Data Structures

Tame the Exaggeration

strings · transformation · iteration

Enthusiastic messages stretch letters: "sooooo goooood!!!!". Write squeeze(s, k) that shortens every run of equal consecutive characters longer than k to exactly k characters. Runs of length k or less are unchanged. The check is case-sensitive and applies to every character, including punctuation and spaces.

Examples

Input:  s = "sooooo goooood!!!!", k = 2
Output: "soo good!!"

Input:  s = "aaa", k = 1
Output: "a"

Input:  s = "abc", k = 3
Output: "abc"

Constraints

  • 0 <= len(s) <= 10**5, printable ASCII
  • 1 <= k <= 100
  • Target: O(n) time

Goals

  • Track the length of the current run while scanning
  • Decide per character whether to keep or drop it
  • Handle a limit that is larger than any run
Starting Python…