Program to find number of squareful arrays in Python

Given a target string and a stamp, we need to find the sequence of stamp positions that can transform a string of question marks into the target string. This is a reverse engineering problem where we work backwards from the target to find valid stamping positions.

Problem Understanding

We start with a sequence of '?' marks and use a stamp to gradually build the target string. The key insight is to work backwards − start from the target string and figure out which stamp positions could have created it.

Algorithm Steps

The algorithm follows these steps ?

  • Handle the special case where stamp has only one character

  • Work backwards from target string, finding valid stamp positions

  • For each possible stamp position, check if it matches part of the remaining target

  • Replace matched portions with '?' marks

  • Continue until the entire string becomes '?' marks

  • Reverse the result since we worked backwards

Implementation

def solve(s, t):
    if len(s) == 1:
        return [i for i in range(len(t))] if all(char == s[0] for char in t) else []

    ans = []
    while t != "?" * len(t):
        tmp = t
        for i in range(len(s)):
            for j in reversed(range(i+1, len(s)+1)):
                search = "?" * i + s[i:j] + "?" * (len(s)-j)
                while t.find(search) != -1:
                    ans.append(t.find(search))
                    t = t.replace(search, "?"*len(s), 1)
                if t == "?" * len(t): 
                    break
            if t == "?" * len(t): 
                break
        if tmp == t: 
            return []
    return ans[::-1]

# Test the function
s = "abcd"
t = "abcdbcd"
result = solve(s, t)
print(f"Stamp: {s}")
print(f"Target: {t}")
print(f"Stamping sequence: {result}")
Stamp: abcd
Target: abcdbcd
Stamping sequence: [3, 0]

How It Works

The algorithm creates search patterns by combining parts of the stamp with '?' marks. For stamp "abcd", it generates patterns like:

  • "abcd" − full stamp

  • "abc?" − partial stamp from beginning

  • "?bcd" − partial stamp from end

  • "?bc?" − partial stamp from middle

Example Walkthrough

For stamp "abcd" and target "abcdbcd" ?

  • Find "?bcd" at position 3: "abcdbcd" ? "abcd???"

  • Find "abcd" at position 0: "abcd???" ? "????"

  • Reverse the sequence: [3, 0]

Edge Cases

# Test with single character stamp
s1 = "a"
t1 = "aaa"
print(f"Single char result: {solve(s1, t1)}")

# Test impossible case
s2 = "abc" 
t2 = "def"
print(f"Impossible case: {solve(s2, t2)}")
Single char result: [0, 1, 2]
Impossible case: []

Conclusion

This algorithm efficiently finds stamp positions by working backwards from the target string. It handles edge cases and returns an empty array when stamping is impossible, making it a robust solution for the stamping problem.

---
Updated on: 2026-03-26T14:30:30+05:30

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements