<p>You are given two strings <code>stamp</code> and <code>target</code>. Initially, there is a string <code>s</code> of length <code>target.length</code> with all <code>s[i] == &#39;?&#39;</code>.</p>

<p>In one turn, you can place <code>stamp</code> over <code>s</code> and replace every letter in the <code>s</code> with the corresponding letter from <code>stamp</code>.</p>

<ul>
	<li>For example, if <code>stamp = &quot;abc&quot;</code> and <code>target = &quot;abcba&quot;</code>, then <code>s</code> is <code>&quot;?????&quot;</code> initially. In one turn you can:

	<ul>
		<li>place <code>stamp</code> at index <code>0</code> of <code>s</code> to obtain <code>&quot;abc??&quot;</code>,</li>
		<li>place <code>stamp</code> at index <code>1</code> of <code>s</code> to obtain <code>&quot;?abc?&quot;</code>, or</li>
		<li>place <code>stamp</code> at index <code>2</code> of <code>s</code> to obtain <code>&quot;??abc&quot;</code>.</li>
	</ul>
	Note that <code>stamp</code> must be fully contained in the boundaries of <code>s</code> in order to stamp (i.e., you cannot place <code>stamp</code> at index <code>3</code> of <code>s</code>).</li>
</ul>

<p>We want to convert <code>s</code> to <code>target</code> using <strong>at most</strong> <code>10 * target.length</code> turns.</p>

<p>Return <em>an array of the index of the left-most letter being stamped at each turn</em>. If we cannot obtain <code>target</code> from <code>s</code> within <code>10 * target.length</code> turns, return an empty array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> stamp = &quot;abc&quot;, target = &quot;ababc&quot;
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong> Initially s = &quot;?????&quot;.
- Place stamp at index 0 to get &quot;abc??&quot;.
- Place stamp at index 2 to get &quot;ababc&quot;.
[1,0,2] would also be accepted as an answer, as well as some other answers.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> stamp = &quot;abca&quot;, target = &quot;aabcaca&quot;
<strong>Output:</strong> [3,0,1]
<strong>Explanation:</strong> Initially s = &quot;???????&quot;.
- Place stamp at index 3 to get &quot;???abca&quot;.
- Place stamp at index 0 to get &quot;abcabca&quot;.
- Place stamp at index 1 to get &quot;aabcaca&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= stamp.length &lt;= target.length &lt;= 1000</code></li>
	<li><code>stamp</code> and <code>target</code> consist of lowercase English letters.</li>
</ul>

class Solution {
public:
//ek stamp ukhaadte hi vapas i=0 se check karna start krre hai..kyuki koi eech ki stamp ukhadne ke baad starting of target me new possibility ban skati hai stmp ukhadne ki

    // Function to check if the stamp can be placed at the current position
    bool check(string stamp, string target_part) {
        int m = stamp.size();
        bool has_match = false;

        for (int i = 0; i < m; i++) {
            if (target_part[i] != '?' && target_part[i] != stamp[i]) {
                return false; // if it doesn't match and it's not a '?', we can't stamp here
            }
            if (target_part[i] == stamp[i]) {
                has_match = true; // at least one character must match directly
            }
        }

        return has_match; // only stamp if at least one character matches
    }

    // Function to replace the target part with '?' where the stamp is placed
    int replace(string &target, string &stamp, int pos) {
        int m = stamp.size();
        int replaced_count = 0;

        for (int i = 0; i < m; i++) {
            if (target[i + pos] != '?') {
                target[i + pos] = '?';
                replaced_count++;
            }
        }
        return replaced_count;
    }

    vector<int> movesToStamp(string stamp, string target) {
        int m = stamp.size();
        int n = target.size();
        int total_replaced = 0; // keep track of total characters replaced
        vector<int> vis(n, 0);   // to keep track of positions where stamp has been placed
        vector<int> result;      // store the moves (positions where stamp was placed)

        // Keep looping until the entire target is replaced
        bool has_stamped = true;

        while (total_replaced < n && has_stamped) {
            has_stamped = false;

            // Try stamping at every position where the stamp fits
            for (int i = 0; i <= n - m; i++) {
                if (!vis[i] && check(stamp, target.substr(i, m))) {
                    vis[i] = 1; // mark this position as stamped
                    int replaced = replace(target, stamp, i);
                    total_replaced += replaced;
                    result.push_back(i); // record this move
                    has_stamped = true;  // we made at least one stamp in this round

                    // Break after stamping once, and restart from i = 0 in the next iteration
                    break;
                }
            }

            // If no stamps are made in an iteration, break the while loop (to avoid infinite loop)
            if (!has_stamped) {
                break;
            }
        }

        // If we couldn't replace the entire target, return an empty vector
        if (total_replaced < n) return {};

        // Reverse the result to get the correct order of moves
        reverse(result.begin(), result.end());
        return result;
    }
};

30400