#K10581. String Transformation via Substring Reversal
String Transformation via Substring Reversal
String Transformation via Substring Reversal
You are given two non-empty strings s
and t
. In one operation, you can select any substring of s
and reverse it. Determine whether it is possible to transform s
into t
by applying any number (possibly zero) of such operations.
Hint: Because reversing substrings can rearrange characters arbitrarily, a necessary and sufficient condition for the transformation is that the two strings have the same multiset of characters. In other words,
$$\text{sorted}(s) = \text{sorted}(t)$$
If the condition holds, output YES
; otherwise, output NO
.
inputFormat
The input is given via stdin and consists of two lines:
- The first line contains the initial string
s
. - The second line contains the target string
t
.
Both strings consist of lowercase letters and are non-empty.
outputFormat
Output to stdout a single line containing either YES
if it is possible to transform s
into t
by reversing substrings any number of times, or NO
otherwise.
abcd
dcba
YES