PalindromeSubsequence
2018 TCO 2C · 2018-04-20 · by majk
Problem Statement
A palindrome is a string that is identical to its reverse. For example, "a", "abba", "aaaaa", and "abbabba" are all palindromes.
A subsequence of a string s is any string of the form s[i1]s[i2]...s[ik] such that the sequence of indices is strictly increasing. (Formally, 0 <= i1 < i2 ... < ik <= |s|-1.)
For example, the strings "", "b", "ace", and "abcde" are all subsequences of the string "abcde", but the strings "bb" and "cba" are not subsequences of "abcde".
A partition of a string s is a set of subsequences that is constructed in such a way that each index into s is used for exactly one of the subsequences.
For example, let s = "aaabb". Then:
- {"aaabb"} is a valid partition of s.
- {"ab", "aab"} is a valid partition of s.
- {"a", "b", "a", "b", "a"} is a valid partition of s.
- {"ab", "abb"} is not a valid partition of s because you would have to use some 'b' multiple times.
- {"ba", "aab"} is not a valid partition of s because "ba" is not a subsequence of s.
- {"a", "a", "b", "b"} is not a valid partition of s because some letter of s remained unused.
The size of a partition is the number of strings it contains. For example, the size of {"aaabb"} is 1, the size of {"ab", "aab"} is 2, and the size of {"a", "ab", "ab"} is 3.
You are given a
- Each string in the partition is a palindrome.
- The size of the partition is as small as possible.
Suppose that you found a solution that uses k subsequences.
Number those subsequences from 1 to k in any order.
Return a
If there are multiple optimal solutions, you may return any of them.
Constraints
- s will contain between 1 and 50 characters, inclusive.
- Every character of s will be either 'a' or 'b'.
"bababba"
Returns: {1, 2, 2, 1, 2, 1, 2 }
Here, it is obvious that there is no valid partition of size 1. There are multiple valid partitions of size 2. The sample output describes one of them, as follows: string s = b a b a b b a return value = 1 2 2 1 2 1 2 ------------------------------ subsequence 1 = b a b subsequence 2 = a b b a In other words, we partitioned the string "bababba" into {"bab", "abba"}. Both "bab" and "abba" are palindromes, so this is a valid solution.
"abba"
Returns: {1, 1, 1, 1 }
Here the input is already palindrome, thus the optimum is 1.
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Returns: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
Returns: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
"abababbababbbabaabbababaa"
Returns: {1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 1, 2, 2, 1, 2, 1, 2, 1, 1 }
Submissions are judged against all 92 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class PalindromeSubsequence with a public method vector<int> optimalPartition(string s) · 92 test cases · 2 s / 256 MB per case