Skip to content

Commit 364f354

Browse files
Merge pull request #1707 from choifi/choiminji
[최민지] Day27
2 parents ac2c2d2 + beda29c commit 364f354

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

leetcode3/최민지/664.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def strangePrinter(self, s: str) -> int:
3+
4+
@cache
5+
def dp(i, j):
6+
# 빈 문자열
7+
if i > j:
8+
return 0
9+
10+
# 문자 하나
11+
if i == j:
12+
return 1
13+
14+
# s[i]를 따로 출력하는 경우
15+
ans = dp(i + 1, j) + 1
16+
17+
# s[i]와 같은 문자를 찾아 같이 출력
18+
for k in range(i + 1, j + 1):
19+
if s[i] == s[k]:
20+
ans = min(ans,
21+
dp(i + 1, k - 1) + dp(k, j))
22+
23+
return ans
24+
25+
return dp(0, len(s) - 1)
26+

0 commit comments

Comments
 (0)