Data Structures & AlgorithmsModerateTCS NQT · string counting
Problem: For the string 'mississippi', what is the number of distinct substrings of length 2 that occur at least twice? Run the code.
Problem: For the string 'mississippi', what is the number of distinct substrings of length 2 that occur at least twice? Run the code.
from collections import Counter s = 'mississippi' c = Counter(s[i:i + 2] for i in range(len(s) - 1)) print(sum(1 for v in c.values() if v >= 2))What is printed?
- A. 2
- B. 4
- C. 3
- D. 5
Correct answer: C. 3
The length-2 substrings are mi, is, ss, si, is, ss, si, ip, pp, pi; the repeated ones are is (2), ss (2) and si (2), so 3 values have count at least 2.
More Coding Interview Problems questions
- Problem: How many Armstrong numbers (numbers equal to the sum of their digits each raised to the power of the digit count) lie in [1…
- Problem: In how many ordered ways can 10 be written as a sum of parts from {1, 2, 3}, where the order of the parts matters? Run the code…
- Problem: How many subsets (including the empty subset) of [1, 2, 3] have XOR equal to 0? Run the code. python a = [1, 2, 3] print(sum(1…
- Problem: What does the following print? python print(bin(10).count('1'), (10).bitlength())
- Problem: How many pairs (i < j) of [2, 3, 4, 5, 6] have gcd(a[i], a[j]) = 1? Run the code. python from math import gcd a = [2, 3, 4, 5, 6]…
- What does this Python program print? python a = [2, -2, 3, -3, 1, 1] p = {0: 1}; s = 0; ans = 0 for x in a: s += x ans += p.get(s, 0) p[s]…