Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61b3df3

Browse files
Time: 360 ms (50.55%), Space: 16.7 MB (54.95%) - LeetHub
1 parent 285bd3d commit 61b3df3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def strangePrinter(self, s: str) -> int:
3+
n = len(s)
4+
dp = [[0] * n for _ in range(n)]
5+
6+
# Base case: Single character
7+
for i in range(n):
8+
dp[i][i] = 1
9+
10+
# Fill the DP table
11+
for length in range(2, n + 1): # length of the substring
12+
for i in range(n - length + 1):
13+
j = i + length - 1
14+
dp[i][j] = dp[i][j-1] + 1 # Start with the worst case
15+
for k in range(i, j):
16+
if s[k] == s[j]:
17+
dp[i][j] = min(dp[i][j], dp[i][k] + (dp[k+1][j-1] if k+1 <= j-1 else 0))
18+
19+
return dp[0][n-1]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /