#K10316. Partition String into Palindromic Substrings
Partition String into Palindromic Substrings
Partition String into Palindromic Substrings
You are given a string S
and a positive integer K
. Your task is to determine if it is possible to partition S
into exactly K non-empty substrings, such that each substring is a palindrome.
A palindrome is a string that reads the same forward and backward. Formally, a string substring
is a palindrome if:
$substring = reverse(substring)$
The primary constraints are:
- If
K > |S|
(the length ofS
), then the answer isNO
since you cannot partition the string into more substrings than its characters. - Let $odd\_count$ be the number of characters in
S
that appear with an odd frequency. Then a necessary condition for the partition to be possible is that $odd\_count \leq K$.
If the conditions are met, output YES
; otherwise, output NO
.
Note: The input is provided via standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The input consists of two lines:
- The first line contains the string
S
consisting of lowercase English letters. - The second line contains an integer
K
representing the number of palindromic substrings to form.
outputFormat
Output a single line containing YES
if it is possible to partition the string into K
palindromic substrings, or NO
otherwise.
aabb
2
YES