-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0005.Longest Palindromic Substring.swift
56 lines (48 loc) · 1.61 KB
/
0005.Longest Palindromic Substring.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
func longestPalindrome(_ s: String) -> String {
let chars = Array(s)
let n = chars.count
var dp = Array(repeating: Array(repeating: -1, count: n), count: n)
var maxLen = 0, s = 0, e = 0
for row in stride(from: n-1, through: 0, by: -1) {
for col in stride(from: row, through: n-1, by: 1) {
if isPalindrome(&dp, chars, from: row, to: col) {
if col-row > maxLen {
maxLen = col-row
s = row
e = col
}
}
}
}
var result = [Character]()
for i in s...e {
result.append(chars[i])
}
let str = String(result)
return str
}
private func isPalindrome(_ dp: inout [[Int]], _ chars: [Character], from row: Int, to col: Int) -> Bool {
if row == col {
dp[row][col] = 1
return true
}
if row+1 > col-1 { // below diagonal
// two chars substring
if chars[col] == chars[row] {
// first char of the string equals to its last char
dp[row][col] = 1
return true
}
}
if dp[row+1][col-1] == 1 {
// previous palindrome exists
if chars[col] == chars[row] {
// first char of the string equals to its last char
dp[row][col] = 1
return true
}
}
return false
}
}