-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcountSubstrings.go
More file actions
66 lines (58 loc) · 1.39 KB
/
Copy pathcountSubstrings.go
File metadata and controls
66 lines (58 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* https://leetcode.com/problems/palindromic-substrings/#/description
Given a string, your task is to count how many palindromic substrings in this string.
The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
Example 1:
Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Note:
The input string length won't exceed 1000.
*/
package lstring
func countSubstrings(s string) int {
countMiddle := func(runes []rune, left, right int) int {
count := 0
for left >= 0 && right < len(runes) && runes[left] == runes[right] {
count += 1
left, right = left-1, right+1
}
return count
}
runes := []rune(s)
if len(runes) == 0 {
return 0
}
count := 0
for i := 0; i < len(runes); i++ {
count += countMiddle(runes, i, i)
count += countMiddle(runes, i, i+1)
}
return count
}
/*
func countSubstrings(s string) int {
isPalindromic := func(s string) bool {
for i, j := 0, len(s)-1; i <= j; {
if s[i] != s[len(s)-1-i] {
return false
}
i++
j--
}
return true
}
res := len(s)
for step := 2; step <= len(s); step++ {
for start := 0; start <= len(s)-step; start++ {
if isPalindromic(s[start : start+step]) {
res++
}
}
}
return res
}
*/