## Similar Problems

Similar Problems not available

# Strange Printer - Leetcode Solution

## Companies:

LeetCode: Strange Printer Leetcode Solution

Difficulty: Hard

Topics: string dynamic-programming

The Strange Printer problem on LeetCode is a dynamic programming problem where we are given a string and the task is to print it without using a new line. However, there are some operations that we can perform on the string to reduce the number of steps required to print it. The problem statement can be found here: https://leetcode.com/problems/strange-printer/.

To solve this problem, we will use dynamic programming. Our approach will be to divide the string into substrings and determine the minimum number of steps required to print each substring. We will store the result in a 2D array.

Let's define our 2D array dp, where dp[i][j] represents the minimum number of steps required to print the substring s[i:j+1]. This means that dp[0][n-1] will contain the final answer.

Now, let's consider how we can fill this array. For every dp[i][j], there are two possible cases:

- Case 1: The last character of the substring s[i:j+1] is printed last.

In this case, we can remove the last character and print the rest of the substring. This will take dp[i][j-1] steps. We will add 1 to this number to account for the last printing step. Therefore, dp[i][j] = dp[i][j-1] + 1.

- Case 2: The last character of the substring s[i:j+1] is printed earlier.

In this case, we can find the first character c in the substring s[i:j] that matches the last character s[j]. We will print the substring s[i:k] using dp[i][k-1] steps, where k is the index of c. Then, we will print the last character s[j] using 1 step. Finally, we will print the rest of the substring s[k+1:j-1] using dp[k+1][j-1] steps. Therefore, dp[i][j] = dp[i][k-1] + 1 + dp[k+1][j-1].

To implement this approach, we will use a nested loop to fill the dp array. The outer loop will iterate over the substring lengths from 1 to n, and the inner loop will iterate over the starting index i. We will use the above two cases to fill the dp array for each substring.

Here is the Python code for the solution:

```
class Solution:
def strangePrinter(self, s: str) -> int:
n = len(s)
dp = [[0] * n for _ in range(n)]
for i in range(n-1, -1, -1):
for j in range(i, n):
if i == j:
dp[i][j] = 1
elif s[i] == s[j]:
dp[i][j] = dp[i][j-1]
else:
dp[i][j] = float('inf')
for k in range(i, j):
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j-1])
dp[i][j] += 1
return dp[0][n-1]
```

The time complexity of this solution is O(n^3), which is not efficient enough for large inputs. However, we can optimize the solution by using memoization. We can store the results of the dp array for each substring in a separate dictionary. This will avoid recalculating the same subproblems multiple times. Here is the optimized Python code for the solution:

```
class Solution:
def strangePrinter(self, s: str) -> int:
n = len(s)
memo = {}
def dp(i, j):
if (i, j) in memo:
return memo[(i, j)]
if i > j:
return 0
res = dp(i, j-1) + 1
for k in range(i, j):
if s[k] == s[j]:
res = min(res, dp(i, k-1) + dp(k+1, j-1))
memo[(i, j)] = res
return res
return dp(0, n-1)
```

The time complexity of the optimized solution is O(n^3), but with memoization, it runs much faster than the previous solution.

## Strange Printer Solution Code

`1`