File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments