Data Structures & AlgorithmsVery hardGATE CS · DSA
Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is printed?
Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is printed?
from collections import Counter
s, k = "ABABBABBABBB", 2
cnt, l, best, mf = Counter(), 0, 0, 0
for r, ch in enumerate(s):
cnt[ch] += 1; mf = max(mf, cnt[ch])
while r - l + 1 - mf > k:
cnt[s[l]] -= 1; l += 1
best = max(best, r - l + 1)
print(best)- A. 8
- B. 9
- C. 6
- D. 11
Correct answer: B. 9
A window is fixable if (length − count of its most frequent letter) ≤ k. The widest such window has length 9.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 3 positions and then LEFT by 4 positions. What is the final array?
- What does this loop-counting code print? (Python) python n, c = 10, 0 for i in range(1, n + 1): j = 1 while j <= n: c += 1 j += i print(c)
- How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed? python from…
- A hash-map two-sum returns the first index pair (i, j), i < j, found while scanning left to right. What is printed? python def twosum(a…
- Evaluate an infix expression of single digits with two stacks (operands and operators, usual precedence, left-associative). What is…
- Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed? python def revk(h, k): cnt…