Build the lexicographically largest string with a limit on repeated letters

Quick Overview

Given a string of lowercase letters and a repeat limit, build the lexicographically largest string from its characters so that no letter appears more than the limit times in a row. It tests greedy choice over letter counts and careful handling of runs when some characters must be left out.

Build the lexicographically largest string with a limit on repeated letters

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a string `s` and an integer `repeat_limit`. Construct the lexicographically largest string using the characters of `s` such that no letter appears more than `repeat_limit` times in a row. You do not have to use all characters from `s`. Return the lexicographically largest string you can construct. ### Function Signature ```python def repeat_limited_string(s: str, repeat_limit: int) -> str: ``` ### Rules - Each character of `s` can be used at most once, and the characters can be placed in any order. - Strings are compared position by position, and the first differing letter decides. If one string is a prefix of the other, the longer string is larger. ### Constraints - `1 <= repeat_limit <= len(s) <= 10^5` - `s` consists of lowercase English letters. ### Examples ```text Input: s = "cczazcc", repeat_limit = 3 Output: "zzcccac" ``` `s` has two `z`, four `c` and one `a`. Both `z` go first. Only three `c` may follow in a row, so the single `a` breaks the run and the fourth `c` comes last. Every character is used. ```text Input: s = "aababab", repeat_limit = 2 Output: "bbabaa" ``` `s` has four `a` and three `b`. Two `b` go first, one `a` breaks the run, and the last `b` follows. The three remaining `a` can only form a run of two at the end, so one `a` is left unused. ```text Input: s = "zzzza", repeat_limit = 1 Output: "zaz" ``` With a limit of 1, every `z` needs a different letter next to it. The single `a` can separate only two `z`, so the other two `z` are dropped.

Overview: Given a string of lowercase letters and a repeat limit, build the lexicographically largest string from its characters so that no letter appears more than the limit times in a row. It tests greedy choice over letter counts and careful handling of runs when some characters must be left out.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s and an integer repeat_limit. Construct the lexicographically largest string using the characters of s such that no letter appears more than repeat_limit times in a row.

You do not have to use all characters from s.

Return the lexicographically largest string you can construct.

Function Signature

def repeat_limited_string(s: str, repeat_limit: int) -> str:

Rules

  • Each character of s can be used at most once, and the characters can be placed in any order.
  • Strings are compared position by position, and the first differing letter decides. If one string is a prefix of the other, the longer string is larger.

Constraints

  • 1 <= repeat_limit <= len(s) <= 10^5
  • s consists of lowercase English letters.

Examples

Input: s = "cczazcc", repeat_limit = 3
Output: "zzcccac"

s has two z, four c and one a. Both z go first. Only three c may follow in a row, so the single a breaks the run and the fourth c comes last. Every character is used.

Input: s = "aababab", repeat_limit = 2
Output: "bbabaa"

s has four a and three b. Two b go first, one a breaks the run, and the last b follows. The three remaining a can only form a run of two at the end, so one a is left unused.

Input: s = "zzzza", repeat_limit = 1
Output: "zaz"

With a limit of 1, every z needs a different letter next to it. The single a can separate only two z, so the other two z are dropped.