Connection Status:
Competition Arena > FindStringEasy
TCO19 SRM 746 · 2019-01-09 · by majk · Dynamic Programming, String Manipulation
Class Name: FindStringEasy
Return Type: String
Method Name: withPalindromicSubstrings
Arg Types: (int)
Problem Statement

Problem Statement

A palindrome is a string which reads the same when reversed. For instance, "aba", "b", and "abba" are palindromes, whereas "baabb", "ab", and "abaaa" are not.

A substring is a nonempty contiguous part of a given string. For instance, "aba" is a substring of "aaba" but "aaa" isn't.

Two substrings are considered different if they are located at different positions in the string. For instance, there are two distinct substrings "aba" in the string "ababa".

You are given an int n. Find and return any string with the following properties:

  • Its length is at most 100.
  • Each character is either 'a' or 'b'.
  • The string has exactly n palindromic substrings.

If there are no such strings, return an empty string instead.

Notes

  • You do not have to minimize the length of the returned string.

Constraints

  • n will be between 1 and 1000, inclusive.
Examples
0)
3
Returns: "aa"

Any valid solution will be accepted. In this case, another correct return value is "bb".

1)
9
Returns: "aaaba"

The nine palindromic substrings of "aaaba" include four copies of "a", two copies of "aa", and one copy each of "b", "aaa", and "aba".

2)
12
Returns: "aabbaba"
3)
20
Returns: "bbbaabbaba"
4)
29
Returns: "aaaaaaab"

Submissions are judged against all 27 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class FindStringEasy with a public method string withPalindromicSubstrings(int n) · 27 test cases · 2 s / 256 MB per case

Submitting as anonymous