FindStringEasy
TCO19 SRM 746 · 2019-01-09 · by majk
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
- 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.
3 Returns: "aa"
Any valid solution will be accepted. In this case, another correct return value is "bb".
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".
12 Returns: "aabbaba"
20 Returns: "bbbaabbaba"
29 Returns: "aaaaaaab"
Submissions are judged against all 27 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
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