mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
24 lines
367 B
Go
24 lines
367 B
Go
package leetcode
|
|
|
|
func countSubstrings(s string) int {
|
|
res := 0
|
|
for i := 0; i < len(s); i++ {
|
|
res += countPalindrome(s, i, i)
|
|
res += countPalindrome(s, i, i+1)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func countPalindrome(s string, left, right int) int {
|
|
res := 0
|
|
for left >= 0 && right < len(s) {
|
|
if s[left] != s[right] {
|
|
break
|
|
}
|
|
left--
|
|
right++
|
|
res++
|
|
}
|
|
return res
|
|
}
|