We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 78c499f commit 0d82bffCopy full SHA for 0d82bff
palindromic-substrings/delight010.swift
@@ -0,0 +1,33 @@
1
+class Solution {
2
+ func countSubstrings(_ s: String) -> Int {
3
+ let charArray = Array(s)
4
+ var count = 0
5
+
6
+ for i in 0..<charArray.count {
7
+ count += countPalindrome(charArray, i, i)
8
+ count += countPalindrome(charArray, i, i + 1)
9
+ }
10
11
+ return count
12
13
14
+ private func countPalindrome(_ s: [Character], _ left: Int, _ right: Int) -> Int {
15
+ var left = left
16
+ var right = right
17
18
19
+ while left >= 0 && right < s.count {
20
+ if s[left] == s[right] {
21
+ count += 1
22
+ } else {
23
+ break
24
25
26
+ left -= 1
27
+ right += 1
28
29
30
31
32
+}
33
0 commit comments