Similar Problems

Similar Problems not available

Move Pieces To Obtain A String - Leetcode Solution

Companies:

LeetCode:  Move Pieces To Obtain A String Leetcode Solution

Difficulty: Medium

Topics: string two-pointers  

Problem Description:

The problem "Move Pieces to Obtain a String" on Leetcode poses the following scenario:

You are given two strings source and target, where source is the original string and target is the desired string. You have an infinite number of moves allowed, where each move consists of moving any character from source to the end of another string s as long as the resulting string is still a valid string. Return true if you can obtain the target string by moves, or false otherwise.

Solution Approach:

We can solve this problem by maintaining two pointers, one for the source string and the other for the target string. We can scan through the target string, and each time we encounter a character that is also present in the source string, we move the source pointer to the right until we find the same character. There are three possible scenarios:

  1. If the character in the source string is the same as the character in the target string, we move both pointers to the right.

  2. If the character in the source string is not the same as the character in the target string, we need to move this character to the end of the source string so that we can use it later. We keep track of these moves.

  3. If we encounter a character in the target string that is not present in the source string, we cannot obtain the target string from the source string using the allowed moves. We return false.

After we have scanned through the entire target string, we check whether the source string is a permutation of the original source string. If it is, we return true. Otherwise, we return false.

Code Implementation:

The code implementation of the above algorithm is as follows:

class Solution {
public:
    bool canConvert(string source, string target) {
        if (source == target) {
            return true;
        }
        unordered_map<char, char> mp;
        for (int i = 0; i < source.size(); ++i) {
            if (mp.count(source[i]) && mp[source[i]] != target[i]) {
                return false;
            }
            mp[source[i]] = target[i];
        }
        unordered_set<char> visited;
        for (auto ch : target) {
            if (!mp.count(ch) || visited.count(ch)) {
                continue;
            }
            visited.insert(ch);
            int j = ch;
            while (j < target.size() && target[j] == ch) {
                ++j;
            }
            int i = source.find(ch);
            while (i != string::npos && i < source.size() && source[i] == ch) {
                i = source.find_first_not_of(ch, i);
            }
            if (j - ch > i - ch) {
                return false;
            }
        }
        return true;
    }
};

Time Complexity:

The time complexity of the above code is O(n), where n is the length of the target string. We scan through the target string only once and perform constant time operations in each iteration.

Space Complexity:

The space complexity of the above code is O(1), since we are using constant extra space throughout the program.

Move Pieces To Obtain A String Solution Code

1