Stamping The Sequence in C++


Suppose we want to make a target string of lowercase letters.

At first, we have the sequence as n '?' marks (n is the length of target string). We also have a stamp of lowercase letters.

On each turn, we can place the stamp over the sequence, and replace every letter in the with the corresponding letter from that stamp. You can make up to 10 * n turns. As an example consider the initial sequence is "?????", and the stamp is "abc", then we may make strings like "abc??", "?abc?", "??abc" in the first turn.

If the sequence is possible to stamp, then return an array of the index with the left-most letter being stamped at each turn. If that is not possible then return an empty array. So when the sequence is "ababc", and the stamp is "abc", then the answer can be like [0, 2], Because we can form like "?????" -> "abc??" -> "ababc".

So, if the input is like stamp = "abcd", target = "abcdbcd", then the output will be [3,0]

To solve this, we will follow these steps −

  • Define an array ret

  • ok := true

  • n := size of stamp

  • tsz := 0

  • while ok is non-zero, do −

    • ok := false

    • x := 0

    • for initialize sz := size of stamp, when sz > 0, update (decrease sz by 1), do −

      • for initialize i := 0, when i <= size of stamp, update (increase i by 1), do −

        • newStamp := a string of '*' of length i + substring of stamp from index i to sz-1 + a string of '*' whose size is same as size of stamp

        • pos := index of newStamp in target

        • while pos is present in target, do −

          • ok := true

          • x := x + sz

          • insert pos at the end of ret

          • fill target from position pos to pos + size of stamp with '*'.

          • pos := index of newStamp in target

    • tsz := tsz + x

  • reverse the array ret

  • return (if tsz is same as size of target, then ret, otherwise one blank array)

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   } cout << "]"<<endl;
}
class Solution {
   public:
   vector<int> movesToStamp(string stamp, string target) {
      vector<int> ret;
      bool ok = true;
      int n = stamp.size();
      int tsz = 0;
      while (ok) {
         ok = false;
         int x = 0;
         for (int sz = stamp.size(); sz > 0; sz--) {
            for (int i = 0; i <= stamp.size() - sz; i++) {
               string newStamp = string(i, '*') +
               stamp.substr(i, sz) + string(stamp.size() - sz - i, '*');
               int pos = target.find(newStamp);
               while (pos != string::npos) {
                  ok = true;
                  x += sz;
                  ret.push_back(pos);
                  fill(target.begin() + pos, target.begin() +
                  pos + stamp.size(), '*');
                  pos = target.find(newStamp);
               }
            }
         }
         tsz += x;
      }
      reverse(ret.begin(), ret.end());
      return tsz == target.size() ? ret : vector<int>();
   }
};
main(){
   Solution ob;
   print_vector(ob.movesToStamp("abcd", "abcdbcd"));
}

Input

"abcd", "abcdbcd"

Output

[3, 0, ]

Updated on: 08-Jun-2020

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements