BackmediumSliding WindowPayPal

Longest Homogeneous Segment Solution

Problem Statement

Given an array of characters and an integer K, find the length of the longest segment that can be made homogeneous by replacing at most K characters.

Example 1
Input
['A', 'B', 'B', 'A', 'A', 'A', 'A', 'A', 'A']
Output
9

Explanation: Step-by-step: we have a segment ['A', 'B', 'B', 'A', 'A', 'A', 'A', 'A', 'A'] with K = 2. We can replace the two 'B's and the last 'A' to make the segment ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'] homogeneous. Therefore, the length of the longest segment is 9.

Example 2
Input
['A', 'A', 'A', 'A', 'A']
Output
5

Explanation: Step-by-step: we have a segment ['A', 'A', 'A', 'A', 'A'] with K = 0. We cannot replace any characters to make the segment homogeneous. Therefore, the length of the longest segment is 5.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of uppercase English letters
  • 0 <= k <= s.length
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Longest Homogeneous Segment — Problem Statement & Solution Guide

Sliding WindowMediumSliding Window / Frequency Map
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of characters and an integer K, find the length of the longest segment that can be made homogeneous by replacing at most K characters.

Examples

Example 1

Input

['A', 'B', 'B', 'A', 'A', 'A', 'A', 'A', 'A']

Output

9

Explanation: Step-by-step: we have a segment ['A', 'B', 'B', 'A', 'A', 'A', 'A', 'A', 'A'] with K = 2. We can replace the two 'B's and the last 'A' to make the segment ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'] homogeneous. Therefore, the length of the longest segment is 9.

Example 2

Input

['A', 'A', 'A', 'A', 'A']

Output

5

Explanation: Step-by-step: we have a segment ['A', 'A', 'A', 'A', 'A'] with K = 0. We cannot replace any characters to make the segment homogeneous. Therefore, the length of the longest segment is 5.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of uppercase English letters
  • 0 <= k <= s.length

Optimal Approach & Strategy

Sliding window. Track max frequency of a single character in the current window. If window length - maxFreq > k, shrink window from left. Max length is the answer. Time O(N), Space O(1) (26 letters).

Brute Force Approach

Check all substrings. Time O(N^2).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function longestHomogeneousSegment(arr, k) { let maxLen = 0; let left = 0; for (let right = 0; right < arr.length; right++) { let count = {}; let replaceCount = 0; for (let i = left; i <= right; i++) { count[arr[i]] = (count[arr[i]] || 0) + 1; if (count[arr[i]] > 1) { replaceCount++; } } while (replaceCount > k) { count[arr[left]]--; if (count[arr[left]] === 0) delete count[arr[left]]; replaceCount--; left++; } maxLen = Math.max(maxLen, right - left + 1); } return maxLen; }

Asked in Top Tech Interviews

PayPal

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.