#K51797. Anagram Substrings Checker
Anagram Substrings Checker
Anagram Substrings Checker
Given a string s, determine whether it can be divided into two equal-length substrings that are anagrams of each other. If the string length is even and the two halves are anagrams, output YES
and the frequency distribution (i.e. count) of the characters in one half. Otherwise, output NO
along with the frequency distribution of the first half of the string.
Important Details:
- If the length of s is odd, it is not possible to split it into two equal parts. In this case, consider the frequency distribution of the first ⌊n/2⌋ characters.
- The frequency distribution should be printed as pairs of character and count, sorted in increasing (alphabetical) order by the character.
- All input is received from standard input and output should be produced to standard output.
Examples:
- For
s = "abab"
: The two halves are "ab" and "ab" which are anagrams. Output:YES
and frequencya 1 b 1
. - For
s = "abcd"
: The halves "ab" and "cd" are not anagrams. Output:NO
and frequency for the first half:a 1 b 1
. - For
s = "abc"
: The string is of odd length. Output:NO
and frequency for the first ⌊3/2⌋=1 character:a 1
.
inputFormat
The input consists of a single line containing the string s.
outputFormat
Output two lines. The first line should be either YES
or NO
. The second line should display the frequency distribution of the relevant half of the string, printed as pairs of character and count separated by a space, with the pairs sorted alphabetically by the character.## sample
abab
YES
a 1 b 1
</p>