go: add «647. Palindromic Substrings»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
54667ca465
commit
16283b93e8
1 changed files with 27 additions and 0 deletions
27
go/palindromic-substrings.go
Normal file
27
go/palindromic-substrings.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
func checkSubstring(s string, i, j int) int {
|
||||
if i < 0 || j >= len(s) {
|
||||
return 0
|
||||
}
|
||||
|
||||
count := 0
|
||||
|
||||
for i >= 0 && j < len(s) && s[i] == s[j] {
|
||||
count += 1
|
||||
|
||||
i -= 1
|
||||
j += 1
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func countSubstrings(s string) int {
|
||||
count := 0
|
||||
|
||||
for i, _ := range s {
|
||||
count += checkSubstring(s, i, i)
|
||||
count += checkSubstring(s, i, i+1)
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
Loading…
Reference in a new issue