Skip to content

Commit e89b509

Browse files
authored
Merge pull request #174 from HmmCorn/liv
가장 긴 팰린드롬
2 parents a2713c7 + 7af71ce commit e89b509

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

  • WEEK16/프로그래머스_가장긴팰린드롬
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 프로그래머스 - 가장 긴 팰린드롬
2+
3+
import Foundation
4+
5+
func solution(_ s: String) -> Int {
6+
let chars = Array(s)
7+
let n = chars.count
8+
9+
var dp = Array(repeating: Array(repeating: false, count: n), count: n)
10+
11+
var answer = 1
12+
13+
for i in 0..<n {
14+
dp[i][i] = true
15+
}
16+
17+
if n >= 2 {
18+
for i in 0..<(n - 1) where chars[i] == chars[i + 1] {
19+
dp[i][i + 1] = true
20+
answer = 2
21+
}
22+
}
23+
24+
if n >= 3 {
25+
for length in 3...n {
26+
for start in 0...(n - length) {
27+
let end = start + length - 1
28+
29+
if chars[start] == chars[end] && dp[start + 1][end - 1] {
30+
dp[start][end] = true
31+
answer = length
32+
}
33+
}
34+
}
35+
}
36+
37+
return answer
38+
}

0 commit comments

Comments
 (0)