Similar Problems

Similar Problems not available

Stamping The Sequence - Leetcode Solution

Companies:

LeetCode:  Stamping The Sequence Leetcode Solution

Difficulty: Hard

Topics: greedy string stack  

Problem Statement:

You have a string S consisting of lowercase English letters. You can perform any number of operations on S. In one operation, you can choose a prefix of S and replace it with any string of the same length consisting of lowercase English letters. You want to perform such a number of operations that makes the string S equal to the string T. Return a list of the minimum number of operations needed to complete this task. If it is not possible, return an empty array.

Example:

Input:

S = "abc", T = "abcbc"

Output:

[0, 1, 2]

Explanation:

Initially: S = "abc" T = "abcbc"

Operation 1: Replace prefix of S with "ab" S = "abbc"

Operation 2: Replace prefix of S with "bc" S = "bcbc"

Now, S is equal to T. Hence, the minimum number of operations required is [0, 1, 2].

Solution:

We can convert string S into string T by performing a series of valid operations on the prefix of S, until it becomes equal to string T. A valid operation is when we replace a prefix of S with the same length prefix of T. For example, if S = "abcde" and T = "xyyde", we can perform a valid operation by replacing prefix "abc" of S with "xyy". The length of prefix for each operation should be same as T's prefix length.

We can make use of a greedy approach here, to perform the above sequence of valid operations. We start by checking if a valid operation can be performed on any prefix of S which results in the maximum possible number of characters in S that are equal to the corresponding prefix of T. In the above example, we can first replace prefix "ab" of S with "xy", because doing so results in the maximum number of characters in S that are equal to the corresponding prefix of T.

We keep performing the above operation on S until we can no longer perform it. We then append the index of the starting position of this operation in S to the result array. We again check if any further valid operation can be performed on S, and we repeat the procedure until S becomes equal to T.

If S cannot be converted to T using the above operations, we return an empty array.

Now, let us implement this approach in python:

class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: # create a list to store the result res = [] # convert the string into a list of chars S = list(target) # set a variable to keep track of the number of stamps not stamped left = len(S) # set a variable to keep track of whether a stamp was stamped # in the previous round of operations changed = True # loop until no stamps can be stamped or the last stamp that was stamped # did not change S while changed and left > 0: # set the value of changed to False initially changed = False # loop through all prefixes of S and check if they can be stamped for start in range(len(S) - len(stamp) + 1): # set a variable to check if the current prefix can be stamped can_replace = True # loop through all characters in the prefix for i in range(len(stamp)): # if the character is already stamped, we cannot replace it if S[start + i] == "": can_replace = False break # if the character is not stamped and is not equal to the current # character of the stamp, we cannot replace it if S[start + i] != stamp[i]: can_replace = False break # if the prefix can be stamped, stamp it and update the values of changed # and left. Also, append the index of the starting position of the stamp # to the result list. if can_replace: changed = True left -= len(stamp) for i in range(len(stamp)): S[start + i] = "" res.append(start) # if S is equal to T, return the reverse list of results if left == 0: return res[::-1] # else, return an empty array else: return []

The time complexity of this solution is O(n^2), where n is the length of the input strings S and T. However, it is possible to optimize this solution further, using a more efficient data structure for checking if a prefix of S can be stamped.

Stamping The Sequence Solution Code

1