#PS0000156. Xâu con dài nhất có thể sắp thành xâu đối xứng (Find Longest Awesome Substring)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Xâu con dài nhất có thể sắp thành xâu đối xứng (Find Longest Awesome Substring)
Find Longest Awesome Substring
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given a digit string , a nonempty contiguous substring qualifies if rearranging its own characters by arbitrary swaps can produce a palindrome, a string identical when read in reverse. No character may be added, removed or replaced. Find the maximum length of a qualifying substring.
Input
One line containing a digit string of length .
Output
Print one integer: the maximum qualifying interval length.
Subtasks
- Subtask 1 (20 points): ; ; characters of belong to
0–9. - Subtask 2 (30 points): ; ; characters of belong to
0,1. - Subtask 3 (50 points): ; ; characters of belong to
0–9.
Example 1
Input
3242415
Output
5
Explanation
Interval [2,6] is 24241, with two 2s, two 4s and one 1; it can become 24142. Both length-six intervals and the whole string have at least two odd digit counts.
Example 2
Input
12345678
Output
1
Explanation
All digits are distinct. Each interval of length at least two has at least two odd digit counts; only singletons qualify.
Example 3
Input
213123
Output
6
Explanation
The whole string contains two of each of 1, 2 and 3 and can be rearranged to 231132. Length 6 is the entire string length.