Data Structures & AlgorithmsModerateTCS NQT Digital · counting
Problem: How many strings of length 4 over {a, b} contain neither 'aa' nor 'bb' as a substring? Run the code.
Problem: How many strings of length 4 over {a, b} contain neither 'aa' nor 'bb' as a substring? Run the code.
from itertools import product
print(sum(1 for t in product('ab', repeat=4) if all(t[i] != t[i + 1] for i in range(3))))What is printed?- A. 4
- B. 3
- C. 1
- D. 2
Correct answer: D. 2
Adjacent letters must differ, so once the first letter is chosen every later letter is forced: 2 × 1 × 1 × 1 = 2 strings, abab and baba. Answering 1 forgets that the string may start with either letter, and 4 wrongly gives some later position a free choice.
More Coding Interview Problems questions
- Problem: What is the sum of Euler's totient phi(k) for k = 1 to 20? Run the code. python from math import gcd print(sum(sum(1 for g in…
- Problem: How many integers in [1, 100] have exactly 4 positive divisors? Run the code. python print(sum(1 for x in range(1, 101) if sum(1…
- Problem: What is the maximum number of pairwise non-overlapping non-empty subarrays of [1, -1, 2, -2, 3, -3] each having sum zero? Run the…
- Problem: How many 3-element subsets of {1, 2, 3, 4, 5, 6} have a sum divisible by 3? Run the code. python from itertools import…
- Problem: Minimum number of deletions so that no two adjacent characters of 'aaabbb' are equal. Run the code. python s = 'aaabbb' kept = []…
- Problem: What is the maximum product of positive integer parts when 10 is split into at least two parts? Run the DP. python n = 10 dp =…