-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
131 Palindrome Partitioning.swift
46 lines (40 loc) 路 1.46 KB
/
131 Palindrome Partitioning.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
//
// 131 Palindrome Partitioning.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 14/12/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/palindrome-partitioning/
class Solution {
/// Finds all possible palindrome partitioning of `s`.
///
/// - Parameter s: A string.
/// - Returns: All possible palindrome.
///
/// - Complexity:
/// - time: O(n * 2^n), where n is the length of `s`.
/// - space: O(n^2), where n is the length of `s`.
func partition(_ s: String) -> [[String]] {
let n = s.count
var dp = [[Bool]](repeating: [Bool](repeating: false, count: n), count: n)
var ans = [[String]]()
var currentList = [String]()
dfs(&ans, str: Array(s), n: n, start: 0, ¤tList, &dp)
return ans
}
private func dfs(_ ans: inout [[String]], str: [Character], n: Int, start: Int, _ currentList: inout [String], _ dp: inout [[Bool]]) {
guard start < n else {
ans.append(currentList)
return
}
for end in start..<n {
guard str[start] == str[end], (end - start <= 2 || dp[start + 1][end - 1]) else { continue }
dp[start][end] = true
currentList.append(String(str[start...end]))
dfs(&ans, str: str, n: n, start: end + 1, ¤tList, &dp)
currentList.removeLast()
}
}
}