Data Structures & AlgorithmsModerateTCS NQT · string round
Problem: How many distinct non-empty substrings does the string 'abab' contain?
Problem: How many distinct non-empty substrings does the string 'abab' contain?
s = 'abab'
print(len({s[i:j] for i in range(len(s)) for j in range(i + 1, len(s) + 1)}))What is printed?- A. 7
- B. 6
- C. 8
- D. 10
Correct answer: A. 7
The distinct substrings are a, b, ab, ba, aba, bab and abab, so 7. Counting all 10 positional substrings ignores duplicates such as the two 'ab' and two 'a' occurrences.
More Coding Interview Problems questions
- Problem: What is the maximum XOR of any two different elements of the array [3, 10, 5, 25, 2, 8]? python a = [3, 10, 5, 25, 2, 8]…
- Problem: What is the length of the longest contiguous subarray containing at most two distinct values? python from collections import…
- Problem: Remove exactly 3 digits from the number '1432219' to make the smallest possible number, keeping the remaining digits in order…
- Problem: What is the length of the longest palindromic subsequence of 'bbbab'? python def lps(s): n = len(s) dp = [[0] n for in range(n)]…
- Problem: Multiply a chain of matrices with dimensions p = [10, 30, 5, 60] (matrix i is p[i] by p[i+1]). Find the minimum number of scalar…
- Problem: How many ways can 10 be formed as an ordered-free sum of coins 1, 2 and 5 (each coin may be used any number of times, and order…