Skip to content

Commit a90f6bc

Browse files
committed
add sol
1 parent a7e4819 commit a90f6bc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

leetcode/daily/3042/sol.go

+33
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)