ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
def pal_sub(s):
n = len(s)
best = ''
for c in range(2 * n - 1):
l, r = c // 2, c // 2 + c % 2
while l >= 0 and r < n and s[l] == s[r]:
l -= 1
r += 1
if r - l - 1 > len(best):
best = s[l + 1:r]
return best
print(pal_sub('babad'), pal_sub('cbbd'))- A. aba bb
- B. bab b
- C. bab bb
- D. cbbd bb
Correct answer: C. bab bb
Expanding around each centre keeps the first palindrome of maximum length. bab is found before aba, and a later palindrome must be strictly longer to replace it.
More Python Programming questions
- What is printed? python def partition(s): last = {ch: i for i, ch in enumerate(s)} out = [] start = end = 0 for i, ch in enumerate(s): end…
- What is printed? python def units(boxes, cap): boxes = sorted(boxes, key=lambda b: -b[1]) total = 0 for n, u in boxes: take = min(n, cap)…
- What is printed? python def minremove(iv): iv = sorted(iv, key=lambda x: x[1]) keep = 0 end = float('-inf') for s, e in iv: if s >= end…
- What is printed? python def longest(s): st = [-1] best = 0 for i, ch in enumerate(s): if ch == '(': st.append(i) else: st.pop() if not st…
- What is printed? python import heapq from collections import Counter words = 'a b c a b a d d d d'.split() c = Counter(words)…
- What is printed? python from functools import cache @cache def cat(n): if n <= 1: return 1 return sum(cat(i) cat(n - 1 - i) for i in…