#C8947. Longest Anagram Substring
Longest Anagram Substring
Longest Anagram Substring
Given two strings S
and T
, your task is to find the length of the longest contiguous substring of S
that is an anagram of some contiguous substring of T
. Two substrings are anagrams if one can be obtained by rearranging the letters of the other. In other words, for substrings s and t, they are anagrams if the frequency of each character in s equals the frequency of that character in t.
For example, if S = "abcab"
and T = "bca"
, one possible answer is 3
because the substring "abc"
of S
is an anagram of T
(since \( a^1b^1c^1 \) equals \( b^1c^1a^1 \)). If no such substring exists, output 0
.
Your solution should read input from standard input (stdin) and output the result to standard output (stdout).
inputFormat
The input consists of two lines:
- The first line contains the string
S
. - The second line contains the string
T
.
outputFormat
Output a single integer representing the length of the longest contiguous substring of S
that is an anagram of some contiguous substring of T
. If no such substring exists, output 0
.
abcab
bca
3