#K53722. Rearrange Substring Problem
Rearrange Substring Problem
Rearrange Substring Problem
You are given two strings s1
and s2
. The task is to determine whether s2
can be formed by rearranging the letters of some contiguous substring of s1
.
In other words, check if there exists a substring in s1
of length equal to s2
, such that it is an anagram of s2
.
Note: Both strings consist of characters, and the order within s2
can be arbitrary as long as the frequency of each character matches.
Mathematical Formulation:
Let f(c, s) denote the frequency of character c in string s. Then, for some starting index i, if the substring s1[i, i+|s2|-1]
satisfies
\[
\forall c,\quad f(c, s1[i, i+|s2|-1]) = f(c, s2),
\]
output YES
. Otherwise, output NO
.
inputFormat
The input consists of two lines. The first line contains the string s1
and the second line contains the string s2
.
outputFormat
Output a single line containing YES
if there exists a substring of s1
whose rearrangement equals s2
, otherwise output NO
.
abcbac
abc
YES