Data Structures & AlgorithmsHardGATE CS · DSA
Overlapping versus non-overlapping substring counts. What is printed?
Overlapping versus non-overlapping substring counts. What is printed?
s, p = "aababaaabbba", "aba" over = sum(1 for i in range(len(s) - len(p) + 1) if s[i:i + len(p)] == p) print(s.count(p), over, s.find(p, 3))
- A. 3 2 3
- B. 2 1 3
- C. 1 2 3
- D. 0 2 3
Correct answer: C. 1 2 3
str.count counts NON-overlapping occurrences scanning left to right, while the window loop counts overlapping ones; find(p, 3) searches from index 3. Output: 1 2 3.
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
- Bars of width 1 have heights [2, 3, 1, 5, 2, 1, 1, 6]. What is the area of the largest rectangle that fits under the histogram (computed…
- For an array of n elements, how many contiguous subarrays are there, and how many subsequences (including the empty one)?
- A monotonic deque reports the maximum of every window of size 3 in [19, 6, 7, 16, 10, 2, 12, 8, 17]. What is the SUM of all reported…
- Two pointers start at both ends of the sorted array [3, 4, 5, 6, 8, 17, 18, 23, 26, 27]. How many pairs (i < j) have a[i] + a[j] ≤ 40?…
- What does this loop-counting code print? (Python) python n, c = 20, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)
- Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is…