-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2083-SubstringsThatBeginAndEndWithTheSameLetter.go
More file actions
74 lines (65 loc) · 2.69 KB
/
2083-SubstringsThatBeginAndEndWithTheSameLetter.go
File metadata and controls
74 lines (65 loc) · 2.69 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
67
68
69
70
71
72
73
74
package main
// 2083. Substrings That Begin and End With the Same Letter
// You are given a 0-indexed string s consisting of only lowercase English letters.
// Return the number of substrings in s that begin and end with the same character.
// A substring is a contiguous non-empty sequence of characters within a string.
// Example 1:
// Input: s = "abcba"
// Output: 7
// Explanation:
// The substrings of length 1 that start and end with the same letter are: "a", "b", "c", "b", and "a".
// The substring of length 3 that starts and ends with the same letter is: "bcb".
// The substring of length 5 that starts and ends with the same letter is: "abcba".
// Example 2:
// Input: s = "abacad"
// Output: 9
// Explanation:
// The substrings of length 1 that start and end with the same letter are: "a", "b", "a", "c", "a", and "d".
// The substrings of length 3 that start and end with the same letter are: "aba" and "aca".
// The substring of length 5 that starts and ends with the same letter is: "abaca".
// Example 3:
// Input: s = "a"
// Output: 1
// Explanation:
// The substring of length 1 that starts and ends with the same letter is: "a".
// Constraints:
// 1 <= s.length <= 10^5
// s consists only of lowercase English letters.
import "fmt"
// 统计字符串中相同字母的数量。则相同字母中任意两个可作为某个子串的首尾字母,
// 另外,还需计算只含一个字母的子串。
func numberOfSubstrings(s string) int64 {
count := make([]int, 26)
for _, v := range s {
count[v-'a']++
}
sum, n := 0, len(s)
for _, v := range count {
sum += (v * (v - 1) >> 1)
}
return int64(sum + n)
}
func main() {
// Example 1:
// Input: s = "abcba"
// Output: 7
// Explanation:
// The substrings of length 1 that start and end with the same letter are: "a", "b", "c", "b", and "a".
// The substring of length 3 that starts and ends with the same letter is: "bcb".
// The substring of length 5 that starts and ends with the same letter is: "abcba".
fmt.Println(numberOfSubstrings("abcba")) // 7
// Example 2:
// Input: s = "abacad"
// Output: 9
// Explanation:
// The substrings of length 1 that start and end with the same letter are: "a", "b", "a", "c", "a", and "d".
// The substrings of length 3 that start and end with the same letter are: "aba" and "aca".
// The substring of length 5 that starts and ends with the same letter is: "abaca".
fmt.Println(numberOfSubstrings("abacad")) // 9
// Example 3:
// Input: s = "a"
// Output: 1
// Explanation:
// The substring of length 1 that starts and ends with the same letter is: "a".
fmt.Println(numberOfSubstrings("a")) // 1
}