Data Structures & AlgorithmsModerateTCS NQT · string counting
Problem: How many distinct palindromic substrings does 'aaaa' have? Run the code.
Problem: How many distinct palindromic substrings does 'aaaa' have? Run the code.
s = 'aaaa'
print(len({s[i:j] for i in range(len(s)) for j in range(i + 1, len(s) + 1) if s[i:j] == s[i:j][::-1]}))What is printed?- A. 10
- B. 4
- C. 5
- D. 6
Correct answer: B. 4
The distinct palindromes are a, aa, aaa and aaaa. Counting all 10 positional substrings ignores that they repeat.
More Coding Interview Problems questions
- Problem: Count the start indices i where a permutation of 'abc' begins in s = 'cbaebabacd' (the anagram-window problem). Run the code…
- Problem: What is the length of the shortest substring of 'aabbcxa' that contains every letter of 'abc'? Run the sliding-window code…
- Problem: What is the length of the longest substring of 'banana' that occurs at least twice (occurrences may overlap)? Run the code…
- Problem: Subset sums of items [1, 2, 3] are computed with a bitset: reach starts as 1 and each item x does reach |= reach << x. How many…
- Problem: Maximum number of events attended when each event is [start, end] (day-inclusive) and you can attend one event per day. Events…
- Problem: How many decimal digits does 100! have? Run the code. python import math print(len(str(math.factorial(100)))) What is printed?