Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: Count the distinct non-empty substrings of "abcabc" (two substrings are the same if their letters are identical, regardless of position).
- A. 21
- B. 16
- C. 15
- D. 12
Correct answer: C. 15
A set of all substrings keeps 3 + 3 + 3 + 3 + 2 + 1 = 15 distinct strings by length. Counting index ranges gives 6·7/2 = 21, adding the empty string gives 16, and dropping the single letters gives 12.
More Coding Interview Problems questions
- Problem: How many windows of length 3 in the text "bacacbaab" are anagrams of the pattern "aab" (same letters with the same…
- Problem: What is the length of the longest substring of "eceebaabbccbdeeabe" that contains at most 2 distinct characters?
- Problem: Find the lexicographically smallest rotation of the string "dcbadcb". What is its starting index (0-based) in the original…
- Problem: The longest common subsequence of "ABCBDAB" and "BDCABA" has length L. What is L?
- Problem: From the string "aaabbbaabbccaa", you may delete the minimum number of characters so that no two adjacent characters are equal…
- Problem: A stack-based reduction processes the string "abbaca" from left to right: if the current character equals the top of the stack…