3.8k views
1 vote
Python

Suppose we have two strings of lower case letters.

In one move we can chose any length and perform following operations

- chose any contigous substring of S of length L and reverse it; at the same chose any contigous substring of string T of length L and reverse it as well.
Note - in one move we can reverse one substring of string S and T of exactly length L.
- Also note that substring borders can be different i.e 1,3 in S and 3,5 in T. Means we can chose same length but position can be anywhere in both string.

So task is to say if it is possible to make string S and T after some sequence of moves.

Example
abcd, abdc - not possible
abab, abab - possible - as already equal.
asdf, asdg - not possible as not equal.
abcd, badc - possible 1 reverse in s and 1 reverse in T.

If possible please explain with time complexity for understanding.

1 Answer

3 votes

Final answer:

The task is to determine if it is possible to create two given strings, S and T, by performing a sequence of moves. The time complexity of this solution is O(n), where n is the length of the strings.

Step-by-step explanation:

The task is to determine if it is possible to create two given strings, S and T, by performing a sequence of moves. In each move, a contiguous substring of S of length L and a contiguous substring of T of length L are chosen and reversed. The goal is to check if it is possible to make both strings equal using these operations.

To solve this problem, we can iterate through both strings simultaneously and keep track of the differences. If both strings have the same characters at a given position, we continue to the next position. However, if the characters are different, we check if the characters can be made equal by reversing their respective substrings.

The time complexity of this solution is O(n), where n is the length of the strings.

User Paul Govan
by
7.1k points