We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a7e4819 commit a90f6bcCopy full SHA for a90f6bc
leetcode/daily/3042/sol.go
@@ -0,0 +1,33 @@
1
+// https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/
2
+
3
+package main
4
5
+import "fmt"
6
7
+func countPrefixSuffixPairs(words []string) int {
8
+ count := 0
9
+ for i := 0; i < len(words); i++ {
10
+ for j := i + 1; j < len(words); j++ {
11
+ if isPrefixAndSuffix(words[i], words[j]) {
12
+ count++
13
+ }
14
15
16
17
+ return count
18
+}
19
20
+func isPrefixAndSuffix(prefixSuffix, word string) bool {
21
+ n := len(prefixSuffix)
22
+ m := len(word)
23
24
+ prefixMatch := m >= n && word[:n] == prefixSuffix
25
+ suffixMatch := m >= n && word[m-n:] == prefixSuffix
26
27
+ return prefixMatch && suffixMatch
28
29
30
+func main() {
31
+ words := []string{"a", "aba", "ababa", "aa"}
32
+ fmt.Println(countPrefixSuffixPairs(words))
33
0 commit comments