Data Structures & AlgorithmsModerateTCS NQT Digital · greedy strings
Problem: Minimum number of deletions so that no two adjacent characters of 'aaabbb' are equal. Run the code.
Problem: Minimum number of deletions so that no two adjacent characters of 'aaabbb' are equal. Run the code.
s = 'aaabbb'
kept = []
for c in s:
if not kept or kept[-1] != c:
kept.append(c)
print(len(s) - len(kept))What is printed?- A. 4
- B. 3
- C. 2
- D. 5
Correct answer: A. 4
The greedy keeps one character from each run, so 'ab' remains and 4 characters are deleted.
More Coding Interview Problems questions
- 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 =…
- Problem: How many integer partitions does 6 have (unordered sums of positive integers equal to 6)? Run the DP. python n = 6 p = [1] + [0]…
- Problem: How many ordered pairs (x, y) with 1 <= x, y <= 10 have x y <= 10? Run the code. python print(sum(1 for x in range(1, 11) for y…
- Problem: How many integers in [1, 1000] contain the digit 0? Run the code. python print(sum(1 for x in range(1, 1001) if '0' in str(x)))…
- Problem: What is the minimum number of perfect squares (1, 4, 9, ...) that sum to 12? Run the DP. python INF = 10 9 dp = [0] + [INF] 12…
- Problem: How many non-decreasing strings of length 3 over the digits {0, 1, 2} exist? Run the code. python from itertools import product…