|
| 1 | +// |
| 2 | +// 647. Palindromic Substrings |
| 3 | +// https://leetcode.com/problems/palindromic-substrings/description/ |
| 4 | +// Dale-Study |
| 5 | +// |
| 6 | +// Created by WhiteHyun on 2024/07/14. |
| 7 | +// |
| 8 | + |
| 9 | +class Solution { |
| 10 | + |
| 11 | + // dp way |
| 12 | + func countSubstringsUsingDP(_ s: String) -> Int { |
| 13 | + let array = Array(s) |
| 14 | + let n = array.count |
| 15 | + var count = 0 |
| 16 | + |
| 17 | + var dp: [[Bool]] = .init( |
| 18 | + repeating: .init(repeating: false, count: n), |
| 19 | + count: n |
| 20 | + ) |
| 21 | + |
| 22 | + for i in dp.indices { |
| 23 | + dp[i][i] = true |
| 24 | + count += 1 |
| 25 | + } |
| 26 | + |
| 27 | + for i in dp.indices.dropLast() where array[i] == array[i + 1] { |
| 28 | + dp[i][i + 1] = true |
| 29 | + count += 1 |
| 30 | + } |
| 31 | + |
| 32 | + for length in stride(from: 3, through: n, by: 1) { |
| 33 | + for i in 0 ... n - length where array[i] == array[i + length - 1] && dp[i + 1][i + length - 2] { |
| 34 | + dp[i][i + length - 1] = true |
| 35 | + count += 1 |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return count |
| 40 | + } |
| 41 | + |
| 42 | + // center and expand way |
| 43 | + func countSubstrings(_ s: String) -> Int { |
| 44 | + let array = Array(s) |
| 45 | + var count = 0 |
| 46 | + |
| 47 | + for startIndex in array.indices { |
| 48 | + let evenCount = expandAroundCenter(array, startIndex, startIndex + 1) |
| 49 | + let oddCount = expandAroundCenter(array, startIndex, startIndex) |
| 50 | + count += evenCount + oddCount |
| 51 | + } |
| 52 | + |
| 53 | + return count |
| 54 | + } |
| 55 | + |
| 56 | + private func expandAroundCenter(_ array: [Character], _ left: Int, _ right: Int) -> Int { |
| 57 | + var count = 0 |
| 58 | + var l = left |
| 59 | + var r = right |
| 60 | + while l >= 0 && r < array.count && array[l] == array[r] { |
| 61 | + l -= 1 |
| 62 | + r += 1 |
| 63 | + count += 1 |
| 64 | + } |
| 65 | + |
| 66 | + return count |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + // Brute Force |
| 71 | + func countSubstringsUsingBruteForce(_ s: String) -> Int { |
| 72 | + let array = Array(s) |
| 73 | + var count = 0 |
| 74 | + |
| 75 | + for startIndex in array.indices { |
| 76 | + for targetIndex in startIndex ..< array.count where array[startIndex] == array[targetIndex] { |
| 77 | + if isPalindrome( |
| 78 | + array, |
| 79 | + startIndex, |
| 80 | + targetIndex |
| 81 | + ) { |
| 82 | + count += 1 |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return count |
| 88 | + } |
| 89 | + |
| 90 | + private func isPalindrome(_ array: [Character], _ start: Int, _ end: Int) -> Bool { |
| 91 | + var startIndex = start |
| 92 | + var endIndex = end |
| 93 | + while startIndex < endIndex { |
| 94 | + if array[startIndex] == array[endIndex] { |
| 95 | + startIndex += 1 |
| 96 | + endIndex -= 1 |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + return false |
| 101 | + } |
| 102 | + |
| 103 | + return true |
| 104 | + } |
| 105 | +} |
0 commit comments