Data Structures & AlgorithmsModerateTCS NQT · string counting
Problem: How many distinct palindromes can be formed by rearranging the letters of 'aabb' (distinct strings, all letters used)? Run the code.
Problem: How many distinct palindromes can be formed by rearranging the letters of 'aabb' (distinct strings, all letters used)? Run the code.
from itertools import permutations
s = 'aabb'
print(len({p for p in map(''.join, permutations(s)) if p == p[::-1]}))What is printed?- A. 1
- B. 2
- C. 4
- D. 0
Correct answer: B. 2
The palindromes are abba and baab. A palindrome needs at most one odd-count letter, and here both letters have even counts, so both arrangements work.
More Coding Interview Problems questions
- Problem: What is the minimum number of deletions to make the string 'abcca' a palindrome? Use the palindromic-subsequence DP and print…
- Problem: Find the next lexicographic permutation of the digits '1243' (the smallest permutation strictly greater than it). Run the code…
- Problem: Find the length of the longest substring of 'AABABBA' that can be made all one letter after at most k = 1 replacement. Run the…
- Problem: How many bits must be flipped to convert A = 29 into B = 15? Run the code. python A, B = 29, 15 print(bin(A ^ B).count('1')) What…
- Problem: What is printed by the stack-based decoder for '3[a2[c]]'? It expands k[s] to s repeated k times. python def decode(s): stack =…
- Problem: What is the minimum number of swaps needed to sort the permutation [4, 3, 2, 1] (values 1..4 each once)? Use the cycle…