Top 150 InterviewЛегко

Longest Repeating Character Replacement

Detailed guide and Python implementation for the 'Longest Repeating Character Replacement' problem.

Постановка задачи

Легко

You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English letter. You can perform this operation at most k times.

Return the length of the longest substring containing the same letter you can get after performing the above operations.

Write a function characterReplacement(s: str, k: int) -> int.

Ограничения
  • 1 <= len(s) <= 10^5
  • s consists of only uppercase English letters
  • 0 <= k <= len(s)

Примеры

Example 1
Input
s = "ABAB", k = 2
Output
4
Explanation

Replace the two 'A's with 'B's or vice versa to get "BBBB" or "AAAA". The longest substring is 4.

Example 2
Input
s = "AABABBA", k = 1
Output
4
Explanation

Replace the 'B' at index 3 with 'A' to get "AAAAABA". The longest substring of same characters starting from index 0 is "AAAA" with length 4.

Need a Hint?
Consider using Sliding Window-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Готовы решить?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Открыть в редакторе
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Рекомендуемые ресурсы Python

Расширьте свои знания с помощью соответствующих интерактивных руководств, шпаргалок и сравнений кода.

Учебник по Python

Циклы Python

Узнайте, как использовать циклы Python для перебора данных. Освойте циклы for, while, прерывание, продолжение и лучшие практики работы с циклами с помощью интерактивных примеров.

Посмотреть ресурс
Практическое руководство

Как заменить символы в строке в Python

Узнайте, как заменять символы или подстроки в строках Python. Освойте метод replace(), лимиты подсчета и использование перевода для нескольких символов.

Посмотреть ресурс
Шпаргалка

Шпаргалка по строковым методам Python

Полное справочное руководство по манипулированию строками в Python. Мастер форматирования, поиска, разделения, замены и проверки свойств строк.

Посмотреть ресурс
Сравнение языков

Python против JavaScript: какой язык программирования лучше?

Всестороннее сравнение Python и JavaScript. Изучите синтаксические различия, производительность, варианты использования (серверная и клиентская части) и примеры кодирования.

Посмотреть ресурс