#C4257. String Transformation with Limited Operations
String Transformation with Limited Operations
String Transformation with Limited Operations
You are given two strings, S1 and S2. Your task is to determine whether it is possible to transform S1 into S2 using at most two operations. An operation is defined as either an insertion or a deletion of a character.
The transformation process works as follows:
- If the absolute difference in lengths of the two strings exceeds 2, the answer is
NO
. - Otherwise, the number of operations is initialized to the absolute difference in lengths, i.e., \( |S_1| - |S_2| \) (in absolute value).
- Then, for the first \( \min(|S_1|,|S_2|) \) characters, compare the characters at each position. For every mismatch, count it as an additional operation.
- If the total number of operations is \( \leq 2 \), print
YES
; otherwise, printNO
.
For example:
Input: S1 = "abc", S2 = "abcd" Output: YES
Note: The comparison is done position-wise starting from the first character.
inputFormat
The first line of input contains an integer T
representing the number of test cases.
Each of the following T
lines contains two space-separated strings: S1 and S2.
It is guaranteed that both strings do not contain any spaces and have a length that makes the problem computationally feasible.
outputFormat
For each test case, output one line containing either YES
if S1 can be transformed into S2 using at most two operations, or NO
otherwise.
5
abc abcd
abcdef abcde
a ab
abcd abefg
abc adc
YES
YES
YES
NO
YES
</p>