Tap here to switch tabs
Problem
Submissions

Problem: Merge Close Characters II

Try to solve the Merge Close Characters II problem.

Statement

You are given a string s and an integer k.

Two characters in s at indices i and j with i<ji < j are considered close if they are identical and j−i≤kj - i \leq k.

As long as there exists at least one pair of close characters, repeatedly perform the following operation:

  1. Find the close pair with the smallest index i. If there is a tie, choose the pair with the smallest index j.

  2. Delete the character at index j from s.

Return the final string after no more operations can be performed.

Constraints:

  • 1≤1 \leq s.length ≤5imes105\leq 5 imes 10^5

  • 1≤1 \leq k ≤\leq s.length

  • s consists of lowercase English letters.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Merge Close Characters II

Try to solve the Merge Close Characters II problem.

Statement

You are given a string s and an integer k.

Two characters in s at indices i and j with i<ji < j are considered close if they are identical and j−i≤kj - i \leq k.

As long as there exists at least one pair of close characters, repeatedly perform the following operation:

  1. Find the close pair with the smallest index i. If there is a tie, choose the pair with the smallest index j.

  2. Delete the character at index j from s.

Return the final string after no more operations can be performed.

Constraints:

  • 1≤1 \leq s.length ≤5imes105\leq 5 imes 10^5

  • 1≤1 \leq k ≤\leq s.length

  • s consists of lowercase English letters.