Skip to content

Commit

Permalink
[DP] Add a solution to Palindromic Substrings
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed May 14, 2018
1 parent 75c9110 commit 0f49a09
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
47 changes: 47 additions & 0 deletions DP/PalindromicSubstrings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Question Link: https://leetcode.com/problems/palindromic-substrings/
* Primary idea: 2D Dynamic Programming, update boolean array based on
* current two characters' equity and the previous boolean subarray
* Time Complexity: O(n^2), Space Complexity: O(n^2)
*
*/

class PalindromicSubstrings {
func countSubstrings(_ s: String) -> Int {
var palinCount = 0, dp = Array(repeating: Array(repeating: false, count: s.count), count: s.count)
var s = Array(s)

// init case with distance of 0 and 1
for i in 0..<s.count {
dp[i][i] = true
palinCount += 1
}

guard s.count > 1 else {
return palinCount
}

for i in 0..<s.count - 1 {
if s[i] == s[i + 1] {
dp[i][i + 1] = true
palinCount += 1
}
}

guard s.count > 2 else {
return palinCount
}

for distance in 2...s.count - 1 {
for i in 0..<s.count - distance {
if s[i] == s[i + distance] && dp[i + 1][i + distance - 1] {
dp[i][i + distance] = true
palinCount += 1
}
}
}

return palinCount

}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 249 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 250 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).


## Array
Expand Down Expand Up @@ -192,6 +192,7 @@
[Coin Change](https://leetcode.com/problems/coin-change/)| [Swift](./DP/CoinChange.swift)| Medium| O(n^2)| O(n)|
[Coin Change II](https://leetcode.com/problems/coin-change-ii/)| [Swift](./DP/CoinChangeII.swift)| Medium| O(n^2)| O(n)|
[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)| [Swift](./DP/LongestIncreasingSubsequence.swift)| Medium| O(n^2)| O(n)|
[Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/)| [Swift](./DP/PalindromicSubstrings.swift)| Medium| O(n^2)| O(n^2)|
[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)| [Swift](./DP/LongestPalindromicSubstring.swift)| Medium| O(n^2)| O(n^2)|
[Perfect Squares](https://leetcode.com/problems/perfect-squares/)| [Swift](./DP/PerfectSquares.swift)| Medium| O(n^2)| O(n)|
[House Robber](https://leetcode.com/problems/house-robber/)| [Swift](./DP/HouseRobber.swift)| Easy| O(n)| O(1)|
Expand Down

0 comments on commit 0f49a09

Please sign in to comment.