ProgrammingHardProduct company OA
How many palindromic substrings does this code count?
How many palindromic substrings does this code count?
const char *s = "abba";
int n = 4, c = 0;
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++) {
int a = i, b = j;
while (a < b && s[a] == s[b]) { a++; b--; }
if (a >= b) c++;
}
printf("%d", c);- A. 6
- B. 5
- C. 10
- D. 4
Correct answer: A. 6
Every substring s[i..j] is tested by moving inward while the ends match. The palindromes are the four single letters, "bb" and "abba": 6. '10' counts all substrings; '5' forgets "abba" or "bb".
More C Programming questions
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0x27; / 10 0111 / printf("%u %u", ~x & (x + 1), x | (x + 1));
- What is printed? c int r0[] = {1, 2}, r1[] = {3, 4, 5}, r2[] = {6}; int rows[] = {r0, r1, r2}; int len[] = {2, 3, 1}; int s = 0; for (int…
- This prints every permutation of "abc". What is the FIFTH string printed? c void p(char s, int k) { if (!s[k]) { printf("%s ", s); return…
- What is printed? c int a = 1; printf("%d %d", a << 2 + 1, (a << 2) + 1 > 4 == 1);
- What is printed? c int a[] = {4, 7, 4, 9, 7, 9, 9}; int x = 0; for (int i = 0; i < 7; i++) x ^= a[i]; printf("%d", x);
- What is printed? c int a[] = {1, 2, 4, 5, 6}, x = 0; for (int i = 1; i <= 6; i++) x ^= i; for (int i = 0; i < 5; i++) x ^= a[i]…