Ctrl K

Longest Letter Run After One Swap

Medium
You are given a string word of lowercase English letters. You may swap any two of its letters, at most once. Return the greatest possible length of a stretch of the string made of a single repeated letter.
Example 1
word="ccdcc" → 4

Swap the d with the last c to get ccccd, with four c in a row. There is no fifth c to fill the gap.

Example 2
word="xyzzyx" → 2

The best you can get is two equal letters in a row, such as the zz already there.

Example 3
word="mmnmmnm" → 5

Swap the first n with the last m: mmmmmnn has five m in a row.

Constraints
  • 1 ≤ len(word) ≤ 2·10⁴
  • word consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumTwo-Letter Stretch MediumRecurring Gene Windows MediumWindow with Few Kinds of Letters MediumEvery Letter at Least k Times Medium
loading Python…
Input
word="ccdcc"
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 59 tests on the server, hidden ones included.