Data Structures & AlgorithmsVery hardFlipkart/Uber OA · placement
For s = "cdbccdddbaddca", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by position) have at most 2 distinct characters?
- A. 5 and 24
- B. 5 and 38
- C. 9 and 38
- D. 4 and 38
Correct answer: B. 5 and 38
Shrink from the left whenever a third letter enters. The longest window is 5. For counting, every right end r contributes (r − left + 1) valid substrings; summing gives 38.
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
- For s = "bcadbdaccdddbd", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by…
- How many contiguous subarrays of [1, 3, 8, 1, 3, 6, 0, 0, 9] have a sum divisible by 3? (Count equal prefix-sum remainders.)
- Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only? python a = [23…
- What does this loop-counting code print? (Python) python n, c = 15, 0 for i in range(1, n + 1): j = 1 while j <= n: c += 1 j += i print(c)
- Longest run of consecutive integers (in any order) using a hash set and starting only from numbers whose predecessor is absent. What is…
- Group words that are anagrams of each other using the sorted word as the key. What is printed? python from collections import defaultdict…