Skip to content

Commit 0d82bff

Browse files
committed
solve problem
1 parent 78c499f commit 0d82bff

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
var count = 0
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+
return count
31+
}
32+
}
33+

0 commit comments

Comments
 (0)