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 = "aabaabbbabab", "bab" 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. 1 1 7
- B. 2 1 7
- C. 1 2 7
- D. 1 3 7
Correct answer: C. 1 2 7
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 7.
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
- Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is…
- A fixed window of length 3 slides over s = "baacbbbcbcabab" with a character-count array. At how many starting positions is the window an…
- Keys are inserted into a hash table of size 10 with h(k) = k mod 10 and quadratic probing (h(k) + i²) mod 10. Which statement is correct?
- What does this loop-counting code print? (Python) python n, c = 64, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)
- A 2-D prefix-sum table answers rectangle queries in O(1). For the 4 × 4 matrix (rows separated by ';', 0-based) 4 8 2 1; 4 3 1 4; 7 6 2 4…
- Which sorting algorithms are stable in their standard array implementations: insertion sort, selection sort, merge sort, heap sort, quick…