#C10770. String Transformation Challenge
String Transformation Challenge
String Transformation Challenge
You are given two strings: source
and target
. Your task is to determine whether the source
string can be transformed into the target
string by applying a series of allowed operations. The allowed operations are:
- Swapping any two characters
- Removing a character
- Adding a character
In other words, you need to check if it is possible to achieve the target
string by reordering the characters in the source
string and performing at most a few additions or removals. A concise way to decide this is to compute the total difference in character frequencies between the two strings. Define the difference as follows:
\[ \text{diff} = \sum_{c}\left| f_S(c) - f_T(c) \right| \]
The transformation is considered possible if and only if diff \le 2
.
For example:
- source = "abc", target = "bca" yields diff = 0 → YES
- source = "abc", target = "def" yields diff = 6 → NO
- source = "abc", target = "abcd" yields diff = 1 → YES
- source = "abcd", target = "ab" yields diff = 2 → YES
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains the
source
string. - The second line contains the
target
string.
outputFormat
Output a single line to standard output (stdout) containing either YES
if the transformation is possible, or NO
otherwise.
abc
bca
YES